ARM: tegra: powermon: Fix copyrights from GPLv3 to GPLv2
[linux-3.10.git] / arch / arm / mach-tegra / tegra_bb.c
1 /*
2  * arch/arm/mach-tegra/tegra_bb.c
3  *
4  * Copyright (C) 2012-2013 NVIDIA Corporation. All rights reserved.
5  *
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 #include <linux/types.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/miscdevice.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/mm.h>
26 #include <linux/io.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/io.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/pm_runtime.h>
34
35 #include <mach/clk.h>
36 #include <mach/tegra_bb.h>
37 #include <mach/tegra_bbc_proxy.h>
38 #include <mach/pm_domains.h>
39 #include <linux/platform_data/nvshm.h>
40
41 #include "clock.h"
42 #include "iomap.h"
43 #include "sleep.h"
44 #include "tegra_emc.h"
45
46 /* BB mailbox offset */
47 #define TEGRA_BB_REG_MAILBOX (0x0)
48 /* BB mailbox return code (MOSD) offset */
49 #define TEGRA_BB_REG_RETCODE (0x4)
50 /* BB status mask */
51 #define TEGRA_BB_STATUS_MASK  (0xffff)
52 #define TEGRA_BB_IPC_COLDBOOT  (0x0001)
53 #define TEGRA_BB_IPC_READY  (0x0005)
54 #define TEGRA_BB_BOOT_RESTART_FW_REQ    (0x0003)
55
56 #define BBC_MC_MIN_FREQ         600000000
57 #define BBC_MC_MAX_FREQ         700000000
58
59 #define APBDEV_PMC_EVENT_COUNTER_0      (0x44c)
60 #define APBDEV_PMC_EVENT_COUNTER_0_EN_MASK      (1<<20)
61 #define APBDEV_PMC_EVENT_COUNTER_0_LP0BB        (0<<16)
62 #define APBDEV_PMC_EVENT_COUNTER_0_LP0ACTIVE    (1<<16)
63
64 #define APBDEV_PMC_IPC_PMC_IPC_STS_0 (0x500)
65 #define APBDEV_PMC_IPC_PMC_IPC_STS_0_AP2BB_RESET_SHIFT (1)
66 #define APBDEV_PMC_IPC_PMC_IPC_STS_0_AP2BB_RESET_DEFAULT_MASK (1)
67 #define APBDEV_PMC_IPC_PMC_IPC_STS_0_BB2AP_MEM_REQ_SHIFT (3)
68 #define APBDEV_PMC_IPC_PMC_IPC_STS_0_BB2AP_MEM_REQ_SOON_SHIFT (4)
69
70 #define APBDEV_PMC_IPC_PMC_IPC_SET_0 (0x504)
71 #define APBDEV_PMC_IPC_PMC_IPC_SET_0_AP2BB_RESET_SHIFT (1)
72 #define APBDEV_PMC_IPC_PMC_IPC_SET_0_AP2BB_MEM_STS_SHIFT (5)
73
74 #define APBDEV_PMC_IPC_PMC_IPC_CLEAR_0 (0x508)
75 #define APBDEV_PMC_IPC_PMC_IPC_CLEAR_0_AP2BB_RESET_SHIFT (1)
76
77 #define FLOW_CTLR_IPC_FLOW_IPC_STS_0 (0x500)
78 #define FLOW_CTLR_IPC_FLOW_IPC_STS_0_AP2BB_MSC_STS_SHIFT  (4)
79 #define FLOW_CTLR_IPC_FLOW_IPC_STS_0_BB2AP_INT1_STS_SHIFT (3)
80 #define FLOW_CTLR_IPC_FLOW_IPC_STS_0_BB2AP_INT0_STS_SHIFT (2)
81 #define FLOW_CTLR_IPC_FLOW_IPC_STS_0_AP2BB_INT0_STS_SHIFT (0)
82
83 #define FLOW_CTLR_IPC_FLOW_IPC_SET_0 (0x504)
84 #define FLOW_CTLR_IPC_FLOW_IPC_SET_0_AP2BB_INT0_STS_SHIFT (0)
85
86 #define FLOW_CTLR_IPC_FLOW_IPC_CLR_0 (0x508)
87 #define FLOW_CTLR_IPC_FLOW_IPC_CLR_0_BB2AP_INT1_STS_SHIFT (3)
88 #define FLOW_CTLR_IPC_FLOW_IPC_CLR_0_BB2AP_INT0_STS_SHIFT (2)
89 #define FLOW_CTLR_IPC_FLOW_IPC_CLR_0_AP2BB_INT0_STS_SHIFT (0)
90
91 #define MC_BBC_MEM_REGIONS_0_OFFSET (0xF0)
92
93 #define MC_BBC_MEM_REGIONS_0_PRIV_SIZE_MASK  (0x3)
94 #define MC_BBC_MEM_REGIONS_0_PRIV_SIZE_SHIFT (0)
95
96 #define MC_BBC_MEM_REGIONS_0_PRIV_BASE_MASK  (0x1FF)
97 #define MC_BBC_MEM_REGIONS_0_PRIV_BASE_SHIFT (3)
98
99 #define MC_BBC_MEM_REGIONS_0_IPC_SIZE_MASK   (0x3)
100 #define MC_BBC_MEM_REGIONS_0_IPC_SIZE_SHIFT  (16)
101
102 #define MC_BBC_MEM_REGIONS_0_IPC_BASE_MASK   (0x1FF)
103 #define MC_BBC_MEM_REGIONS_0_IPC_BASE_SHIFT  (19)
104
105 enum bbc_pm_state {
106         BBC_REMOVE_FLOOR = 1,
107         BBC_SET_FLOOR,
108         BBC_CRASHDUMP_FLOOR,
109 };
110
111 struct tegra_bb {
112         spinlock_t lock;
113         int status;
114         int instance;
115         char name[16];
116         char priv_name[16];
117         char ipc_name[16];
118         unsigned long priv_phy;
119         unsigned long ipc_phy;
120         void *ipc_virt;
121         void *mb_virt;
122         unsigned long priv_size;
123         unsigned long ipc_size;
124         unsigned long ipc_irq;
125         unsigned long emc_min_freq;
126         u32 emc_flags;
127         char ipc_serial[NVSHM_SERIAL_BYTE_SIZE];
128         unsigned int irq;
129         unsigned int mem_req_soon;
130         enum bbc_pm_state state, prev_state;
131         struct regulator *vdd_bb_core;
132         struct regulator *vdd_bb_pll;
133         void (*ipc_cb)(void *data);
134         void  *ipc_cb_data;
135         struct miscdevice dev_priv;
136         struct miscdevice dev_ipc;
137         struct device *dev;
138         struct sysfs_dirent *sd;
139         struct nvshm_platform_data nvshm_pdata;
140         struct platform_device nvshm_device;
141         struct workqueue_struct *workqueue;
142         struct work_struct work;
143         struct clk *emc_clk;
144         struct device *proxy_dev;
145 };
146
147
148 static int tegra_bb_open(struct inode *inode, struct file *filp);
149 static int tegra_bb_map(struct file *filp, struct vm_area_struct *vma);
150 static int tegra_bb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
151
152 static const struct file_operations tegra_bb_priv_fops = {
153         .owner          = THIS_MODULE,
154         .open           = tegra_bb_open,
155         .mmap           = tegra_bb_map,
156 };
157
158 static const struct file_operations tegra_bb_ipc_fops = {
159         .owner          = THIS_MODULE,
160         .open           = tegra_bb_open,
161         .mmap           = tegra_bb_map,
162 };
163
164 static const struct vm_operations_struct tegra_bb_vm_ops = {
165         .fault = tegra_bb_vm_fault,
166 };
167
168 void tegra_bb_register_ipc(struct platform_device *pdev,
169                            void (*cb)(void *data), void *cb_data)
170 {
171         struct tegra_bb *bb;
172         struct tegra_bb_platform_data *pdata;
173
174         if (!pdev) {
175                 pr_err("%s platform device is NULL!\n", __func__);
176                 return;
177         }
178
179         pdata = pdev->dev.platform_data;
180
181         if (!pdata) {
182                 dev_err(&pdev->dev, "%s platform dev not found!\n", __func__);
183                 return;
184         }
185
186         bb = (struct tegra_bb *)pdata->bb_handle;
187
188         if (!bb) {
189                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
190                 return;
191         }
192         bb->ipc_cb = cb;
193         bb->ipc_cb_data = cb_data;
194 }
195 EXPORT_SYMBOL_GPL(tegra_bb_register_ipc);
196
197 void tegra_bb_generate_ipc(struct platform_device *pdev)
198 {
199         struct tegra_bb *bb;
200 #ifndef CONFIG_TEGRA_BASEBAND_SIMU
201         void __iomem *flow = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
202 #endif
203         struct tegra_bb_platform_data *pdata;
204
205         if (!pdev) {
206                 pr_err("%s platform device is NULL!\n", __func__);
207                 return;
208         }
209
210         pdata = pdev->dev.platform_data;
211
212         if (!pdata) {
213                 dev_err(&pdev->dev, "%s platform dev not found!\n", __func__);
214                 return;
215         }
216
217         bb = (struct tegra_bb *)pdata->bb_handle;
218
219         if (!bb) {
220                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
221                 return;
222         }
223
224 #ifdef CONFIG_TEGRA_BASEBAND_SIMU
225         {
226                 int sts;
227                 if (bb->ipc_cb)
228                         bb->ipc_cb(bb->ipc_cb_data);
229                 /* Notify sysfs */
230                 sts = *(unsigned int *)bb->mb_virt & TEGRA_BB_STATUS_MASK;
231
232                 if ((bb->status != TEGRA_BB_IPC_READY) ||
233                     (bb->status != sts)) {
234                         sysfs_notify(&bb->dev->kobj, NULL, "status");
235                 }
236                 bb->status = sts;
237         }
238 #else
239         {
240                 u32 sts = readl(flow + FLOW_CTLR_IPC_FLOW_IPC_STS_0);
241                 sts |= 1 << FLOW_CTLR_IPC_FLOW_IPC_SET_0_AP2BB_INT0_STS_SHIFT;
242                 writel(sts, flow + FLOW_CTLR_IPC_FLOW_IPC_SET_0);
243         }
244 #endif
245 }
246 EXPORT_SYMBOL_GPL(tegra_bb_generate_ipc);
247
248 void tegra_bb_clear_ipc(struct platform_device *dev)
249 {
250         void __iomem *fctrl = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
251
252         /* clear BB2AP INT status */
253         writel(1 << FLOW_CTLR_IPC_FLOW_IPC_CLR_0_BB2AP_INT0_STS_SHIFT,
254                fctrl + FLOW_CTLR_IPC_FLOW_IPC_CLR_0);
255 }
256 EXPORT_SYMBOL_GPL(tegra_bb_clear_ipc);
257
258 void tegra_bb_abort_ipc(struct platform_device *dev)
259 {
260         void __iomem *fctrl = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
261
262         /* clear AP2BB INT status */
263         writel(1 << FLOW_CTLR_IPC_FLOW_IPC_CLR_0_AP2BB_INT0_STS_SHIFT,
264                fctrl + FLOW_CTLR_IPC_FLOW_IPC_CLR_0);
265 }
266 EXPORT_SYMBOL_GPL(tegra_bb_abort_ipc);
267
268 int tegra_bb_check_ipc(struct platform_device *dev)
269 {
270         void __iomem *fctrl = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
271         int sts;
272
273         sts = readl(fctrl + FLOW_CTLR_IPC_FLOW_IPC_STS_0);
274         if (sts & (1 << FLOW_CTLR_IPC_FLOW_IPC_STS_0_AP2BB_INT0_STS_SHIFT))
275                 return 0;
276         return 1;
277 }
278 EXPORT_SYMBOL_GPL(tegra_bb_check_ipc);
279
280 static int tegra_bb_open(struct inode *inode, struct file *filp)
281 {
282         int ret;
283
284         ret = nonseekable_open(inode, filp);
285         return ret;
286 }
287
288 static int tegra_bb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
289 {
290         pr_warn("%s  vma->vm_start=0x%x vma->vm_end=0x%x vma->vm_pgoff=0x%x\n"
291                 , __func__,
292                 (unsigned int)vma->vm_start,
293                 (unsigned int)vma->vm_end,
294                 (unsigned int)vma->vm_pgoff);
295         vmf = vmf;
296         return VM_FAULT_NOPAGE;
297 }
298
299 static inline void tegra_bb_enable_mem_req_soon(void)
300 {
301         void __iomem *fctrl = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
302         int val = readl(fctrl + FLOW_CTLR_IPC_FLOW_IPC_STS_0);
303
304         /* AP2BB_MSC_STS[3] is to mask or unmask
305          * mem_req_soon interrupt to interrupt controller */
306         val = val | (0x8 << FLOW_CTLR_IPC_FLOW_IPC_STS_0_AP2BB_MSC_STS_SHIFT);
307         writel(val, fctrl + FLOW_CTLR_IPC_FLOW_IPC_SET_0);
308
309         pr_debug("%s: fctrl ipc_sts = %x\n", __func__, val);
310 }
311
312 static int tegra_bb_map(struct file *filp, struct vm_area_struct *vma)
313 {
314         struct miscdevice *miscdev = filp->private_data;
315         struct tegra_bb *bb;
316         unsigned long phy;
317         unsigned long off;
318         unsigned long vsize;
319         unsigned long psize;
320         int ret;
321
322         off = vma->vm_pgoff << PAGE_SHIFT;
323         vsize = vma->vm_end - vma->vm_start;
324         bb = dev_get_drvdata(miscdev->parent);
325         if (miscdev->fops == &tegra_bb_priv_fops) {
326                 phy = bb->priv_phy + off;
327                 pr_debug("%s map priv section @0x%x\n",
328                         __func__,
329                         (unsigned int)phy);
330                 psize = bb->priv_size - off;
331         } else {
332                 phy = bb->ipc_phy + off;
333                 pr_debug("%s map ipc section @0x%x\n",
334                         __func__,
335                         (unsigned int)phy);
336                 psize =  bb->ipc_size - off;
337         }
338         if (vsize > psize) {
339                 pr_err("%s request exceed mapping!\n",
340                        __func__);
341                 return -EINVAL;
342         }
343
344         vma->vm_ops = &tegra_bb_vm_ops;
345         ret = remap_pfn_range(vma, vma->vm_start, __phys_to_pfn(phy),
346                               vsize, pgprot_noncached(vma->vm_page_prot));
347         if (ret) {
348                 pr_err("%s remap_pfn_range ret %d\n", __func__, (int)ret);
349                 return -EAGAIN;
350         }
351         return ret;
352 }
353
354 static ssize_t show_tegra_bb_retcode(struct device *dev,
355                                   struct device_attribute *attr, char *buf)
356 {
357         struct tegra_bb *bb;
358         int retcode;
359         struct platform_device *pdev = container_of(dev,
360                                                     struct platform_device,
361                                                     dev);
362         struct tegra_bb_platform_data *pdata;
363
364         if (!pdev) {
365                 dev_err(dev, "%s platform device is NULL!\n", __func__);
366                 return 0;
367         }
368
369         pdata = pdev->dev.platform_data;
370
371         if (!pdata) {
372                 dev_err(&pdev->dev, "%s platform data not found!\n", __func__);
373                 return 0;
374         }
375
376         bb = (struct tegra_bb *)pdata->bb_handle;
377
378         if (!bb) {
379                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
380                 return 0;
381         }
382
383         retcode = *(int *)((unsigned long)bb->mb_virt + TEGRA_BB_REG_RETCODE);
384         dev_dbg(&pdev->dev, "%s retcode=%d\n", __func__, (int)retcode);
385         return sprintf(buf, "%d\n", retcode);
386 }
387 static DEVICE_ATTR(retcode, S_IRUSR | S_IRGRP, show_tegra_bb_retcode, NULL);
388
389 static ssize_t show_tegra_bb_status(struct device *dev,
390                                      struct device_attribute *attr, char *buf)
391 {
392         struct tegra_bb *bb;
393         unsigned int *ptr;
394         int status;
395         struct platform_device *pdev = container_of(dev,
396                                                     struct platform_device,
397                                                     dev);
398         struct tegra_bb_platform_data *pdata;
399
400         if (!pdev) {
401                 dev_err(dev, "%s platform device is NULL!\n", __func__);
402                 return 0;
403         }
404
405         pdata = pdev->dev.platform_data;
406
407         if (!pdata) {
408                 dev_err(&pdev->dev, "%s platform data not found!\n", __func__);
409                 return 0;
410         }
411
412         bb = (struct tegra_bb *)pdata->bb_handle;
413
414         if (!bb) {
415                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
416                 return 0;
417         }
418
419         ptr = bb->mb_virt + TEGRA_BB_REG_MAILBOX;
420         status = *ptr & 0xFFFF;
421         dev_dbg(&pdev->dev, "%s status=%x\n", __func__,
422                  status);
423         return sprintf(buf, "%d\n", status);
424 }
425
426 static ssize_t store_tegra_bb_status(struct device *dev,
427                                       struct device_attribute *attr,
428                                       const char *buf, size_t count)
429 {
430         struct tegra_bb *bb;
431         int ret, value;
432         unsigned int *ptr;
433         struct platform_device *pdev = container_of(dev,
434                                                     struct platform_device,
435                                                     dev);
436         struct tegra_bb_platform_data *pdata;
437
438         if (!pdev) {
439                 dev_err(dev, "%s platform device is NULL!\n", __func__);
440                 return 0;
441         }
442
443         pdata = pdev->dev.platform_data;
444
445         if (!pdata) {
446                 dev_err(&pdev->dev, "%s platform data not found!\n", __func__);
447                 return 0;
448         }
449
450         bb = (struct tegra_bb *)pdata->bb_handle;
451
452         if (!bb) {
453                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
454                 return 0;
455         }
456
457         ret = sscanf(buf, "%d", &value);
458         if (ret != 1)
459                 return -1;
460         value &= 0xFFFF;
461         ptr = bb->mb_virt + TEGRA_BB_REG_MAILBOX;
462         *ptr = (value & 0xFFFF) | ((~value << 16) & 0xFFFF0000);
463         tegra_bb_generate_ipc(pdev);
464         dev_dbg(&pdev->dev, "%s status=0x%x\n",
465                  __func__,
466                  (unsigned int)value);
467         return count;
468 }
469
470 static DEVICE_ATTR(status, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
471                    show_tegra_bb_status, store_tegra_bb_status);
472
473 static ssize_t show_tegra_bb_priv_size(struct device *dev,
474                                     struct device_attribute *attr,
475                                     char *buf)
476 {
477         struct tegra_bb *bb;
478         struct platform_device *pdev = container_of(dev,
479                                                     struct platform_device,
480                                                     dev);
481         struct tegra_bb_platform_data *pdata;
482
483         if (!pdev) {
484                 dev_err(dev, "%s platform device is NULL!\n", __func__);
485                 return 0;
486         }
487
488         pdata = pdev->dev.platform_data;
489
490         if (!pdata) {
491                 dev_err(&pdev->dev, "%s platform data not found!\n", __func__);
492                 return 0;
493         }
494
495         bb = (struct tegra_bb *)pdata->bb_handle;
496
497         if (!bb) {
498                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
499                 return 0;
500         }
501
502         dev_dbg(&pdev->dev, "%s priv_size=%d\n", __func__,
503                  (unsigned int)bb->priv_size);
504         return sprintf(buf, "%d\n", (int)bb->priv_size);
505 }
506 static DEVICE_ATTR(priv_size, S_IRUSR | S_IRGRP, show_tegra_bb_priv_size, NULL);
507
508 static ssize_t show_tegra_bb_ipc_size(struct device *dev,
509                                       struct device_attribute *attr,
510                                       char *buf)
511 {
512         struct tegra_bb *bb;
513         struct platform_device *pdev = container_of(dev,
514                                                     struct platform_device,
515                                                     dev);
516         struct tegra_bb_platform_data *pdata;
517
518         if (!pdev) {
519                 dev_err(dev, "%s platform device is NULL!\n", __func__);
520                 return 0;
521         }
522
523         pdata = pdev->dev.platform_data;
524
525         if (!pdata) {
526                 dev_err(&pdev->dev, "%s platform data not found!\n", __func__);
527                 return 0;
528         }
529
530         bb = (struct tegra_bb *)pdata->bb_handle;
531
532         if (!bb) {
533                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
534                 return 0;
535         }
536
537         dev_dbg(&pdev->dev, "%s ipc_size=%d\n",
538                  __func__,
539                  (unsigned int)bb->ipc_size);
540         return sprintf(buf, "%d\n", (int)bb->ipc_size);
541 }
542 static DEVICE_ATTR(ipc_size, S_IRUSR | S_IRGRP, show_tegra_bb_ipc_size, NULL);
543
544 static ssize_t store_tegra_bb_reset(struct device *dev,
545                                       struct device_attribute *attr,
546                                       const char *buf, size_t count)
547 {
548         int ret, value;
549         static bool regulator_status;
550         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
551         struct platform_device *pdev = container_of(dev, struct platform_device,
552                                                     dev);
553         struct tegra_bb_platform_data *pdata;
554         struct tegra_bb *bb;
555
556         if (!pdev) {
557                 dev_err(dev, "%s platform device is NULL\n", __func__);
558                 return 0;
559         }
560
561         pdata = pdev->dev.platform_data;
562         if (!pdata) {
563                 dev_err(&pdev->dev, "%s platform data not found\n", __func__);
564                 return 0;
565         }
566
567         bb = (struct tegra_bb *)pdata->bb_handle;
568         if (!bb) {
569                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
570                 return 0;
571         }
572
573         ret = sscanf(buf, "%d", &value);
574
575         if (ret != 1)
576                 return -1;
577
578         if ((value < 0) || (value > 1))
579                 return -EINVAL;
580
581         dev_dbg(&pdev->dev, "%s reset=%d\n",
582                  __func__,
583                  (unsigned int)value);
584
585         /* reset is active low - sysfs interface assume reset is active high */
586         if (value) {
587                 writel(1 << APBDEV_PMC_IPC_PMC_IPC_CLEAR_0_AP2BB_RESET_SHIFT |
588                        1 << APBDEV_PMC_IPC_PMC_IPC_SET_0_AP2BB_MEM_STS_SHIFT,
589                         pmc + APBDEV_PMC_IPC_PMC_IPC_CLEAR_0);
590
591                 if (regulator_status == true) {
592                         pr_debug("%s: disabling bbc regulators\n", __func__);
593                         regulator_disable(bb->vdd_bb_core);
594                         regulator_disable(bb->vdd_bb_pll);
595
596                         regulator_status = false;
597                 }
598
599                 bb->state = BBC_REMOVE_FLOOR;
600                 queue_work(bb->workqueue, &bb->work);
601         } else {
602                 /* power on bbc rails */
603                 if (bb->vdd_bb_core && bb->vdd_bb_pll &&
604                                 regulator_status == false) {
605                         pr_debug("%s: enabling bbc regulators\n", __func__);
606                         regulator_set_voltage(bb->vdd_bb_core, 1100000,
607                                                         1100000);
608                         regulator_enable(bb->vdd_bb_core);
609
610                         regulator_set_voltage(bb->vdd_bb_pll, 1100000,
611                                                         1100000);
612                         regulator_enable(bb->vdd_bb_pll);
613                         regulator_status = true;
614
615                         tegra_bb_enable_mem_req_soon();
616                 }
617
618                 writel(1 << APBDEV_PMC_IPC_PMC_IPC_SET_0_AP2BB_RESET_SHIFT |
619                        1 << APBDEV_PMC_IPC_PMC_IPC_SET_0_AP2BB_MEM_STS_SHIFT,
620                         pmc + APBDEV_PMC_IPC_PMC_IPC_SET_0);
621         }
622
623         return ret;
624 }
625
626 static ssize_t show_tegra_bb_reset(struct device *dev,
627                                       struct device_attribute *attr,
628                                       char *buf)
629 {
630         int sts;
631         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
632         struct platform_device *pdev = container_of(dev,
633                                                     struct platform_device,
634                                                     dev);
635         /* reset is active low - sysfs interface assume reset is active high */
636
637         sts = readl(pmc + APBDEV_PMC_IPC_PMC_IPC_STS_0);
638         dev_dbg(&pdev->dev, "%s IPC_STS=0x%x\n",
639                  __func__,
640                  (unsigned int)sts);
641         sts = ~sts >> APBDEV_PMC_IPC_PMC_IPC_STS_0_AP2BB_RESET_SHIFT;
642         sts &= APBDEV_PMC_IPC_PMC_IPC_STS_0_AP2BB_RESET_DEFAULT_MASK;
643
644         dev_dbg(&pdev->dev, "%s reset=%d\n",
645                  __func__,
646                  (unsigned int)sts);
647         return sprintf(buf, "%d\n", (int)sts);
648 }
649
650 static DEVICE_ATTR(reset, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
651                    show_tegra_bb_reset, store_tegra_bb_reset);
652
653 static ssize_t show_tegra_bb_state(struct device *dev,
654                                       struct device_attribute *attr,
655                                       char *buf)
656 {
657         unsigned int sts, mem_req, mem_req_soon;
658         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
659         struct platform_device *pdev = container_of(dev,
660                                                     struct platform_device,
661                                                     dev);
662         sts = readl(pmc + APBDEV_PMC_IPC_PMC_IPC_STS_0);
663         dev_dbg(&pdev->dev, "%s IPC_STS=0x%x\n", __func__,
664                          (unsigned int)sts);
665
666         mem_req = (sts >> APBDEV_PMC_IPC_PMC_IPC_STS_0_BB2AP_MEM_REQ_SHIFT) & 1;
667         mem_req_soon =
668                 (sts >> APBDEV_PMC_IPC_PMC_IPC_STS_0_BB2AP_MEM_REQ_SOON_SHIFT)
669                 & 1;
670
671         dev_dbg(&pdev->dev, "%s mem_req=%d mem_req_soon=%d\n", __func__,
672                                         mem_req, mem_req_soon);
673         return sprintf(buf, "%d\n", (unsigned int)mem_req);
674 }
675
676 static DEVICE_ATTR(state, S_IRUSR | S_IRGRP, show_tegra_bb_state, NULL);
677
678 static ssize_t show_tegra_bb_serial(struct device *dev,
679                                       struct device_attribute *attr, char *buf)
680 {
681         struct tegra_bb *bb;
682         struct platform_device *pdev = container_of(dev,
683                                                     struct platform_device,
684                                                     dev);
685         struct tegra_bb_platform_data *pdata;
686         int idx, ret;
687
688         if (!pdev) {
689                 dev_err(dev, "%s platform device is NULL!\n", __func__);
690                 return 0;
691         }
692
693         pdata = pdev->dev.platform_data;
694
695         if (!pdata) {
696                 dev_err(&pdev->dev, "%s platform data not found!\n", __func__);
697                 return 0;
698         }
699
700         bb = (struct tegra_bb *)pdata->bb_handle;
701
702         if (!bb) {
703                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
704                 return 0;
705         }
706
707         for (idx = 0; NVSHM_SERIAL_BYTE_SIZE > idx; ++idx) {
708                 ret = sprintf(buf+2*idx, "%02X", bb->ipc_serial[idx]);
709                 if (ret < 0) {
710                         dev_err(&pdev->dev, "%s sprintf shm serial failure!\n",
711                                         __func__);
712                         return 0;
713                 }
714         }
715
716         buf[2*NVSHM_SERIAL_BYTE_SIZE] = '\n';
717
718         return (2*NVSHM_SERIAL_BYTE_SIZE+1);
719 }
720
721 static DEVICE_ATTR(serial, S_IRUSR | S_IRGRP, show_tegra_bb_serial, NULL);
722
723 void tegra_bb_set_ipc_serial(struct platform_device *pdev, char *serial)
724 {
725         struct tegra_bb *bb;
726         struct tegra_bb_platform_data *pdata;
727
728         if (!pdev) {
729                 pr_err("%s platform device is NULL!\n", __func__);
730                 return;
731         }
732
733         pdata = pdev->dev.platform_data;
734
735         if (!pdata) {
736                 dev_err(&pdev->dev, "%s platform dev not found!\n", __func__);
737                 return;
738         }
739
740         bb = (struct tegra_bb *)pdata->bb_handle;
741
742         if (!bb) {
743                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
744                 return;
745         }
746
747         if (serial == NULL) {
748                 /* Remove sysfs entry */
749                 device_remove_file(&pdev->dev, &dev_attr_serial);
750                 return;
751         }
752
753         /* Create sysfs entry */
754         device_create_file(&pdev->dev, &dev_attr_serial);
755
756         /* Locally store serail number for future sysfs access */
757         memcpy(bb->ipc_serial, serial, sizeof(bb->ipc_serial));
758 }
759 EXPORT_SYMBOL_GPL(tegra_bb_set_ipc_serial);
760
761 #ifndef CONFIG_TEGRA_BASEBAND_SIMU
762 static irqreturn_t tegra_bb_isr_handler(int irq, void *data)
763 {
764         struct tegra_bb *bb = (struct tegra_bb *)data;
765         void __iomem *fctrl = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
766         unsigned long irq_flags;
767         int sts;
768
769         disable_irq_nosync(irq);
770
771         spin_lock_irqsave(&bb->lock, irq_flags);
772         /* read/clear INT status */
773         sts = readl(fctrl + FLOW_CTLR_IPC_FLOW_IPC_STS_0);
774         if (sts & (1 << FLOW_CTLR_IPC_FLOW_IPC_STS_0_BB2AP_INT0_STS_SHIFT)) {
775                 writel(1 << FLOW_CTLR_IPC_FLOW_IPC_CLR_0_BB2AP_INT0_STS_SHIFT,
776                        fctrl + FLOW_CTLR_IPC_FLOW_IPC_CLR_0);
777         }
778         spin_unlock_irqrestore(&bb->lock, irq_flags);
779         /* wake IPC mechanism */
780         if (bb->ipc_cb)
781                 bb->ipc_cb(bb->ipc_cb_data);
782
783         /* Notify sysfs */
784         sts = *(unsigned int *)bb->mb_virt & TEGRA_BB_STATUS_MASK;
785
786         if ((bb->status != TEGRA_BB_IPC_READY) ||
787             (bb->status != sts)) {
788                 pr_debug("%s: notify sysfs status %d\n", __func__, sts);
789                 sysfs_notify_dirent(bb->sd);
790         }
791
792         if (sts == TEGRA_BB_BOOT_RESTART_FW_REQ) {
793                 pr_debug("%s: boot_restart_fw_req\n", __func__);
794                 bb->state = BBC_CRASHDUMP_FLOOR;
795                 queue_work(bb->workqueue, &bb->work);
796         }
797
798         bb->status = sts;
799         return IRQ_HANDLED;
800 }
801
802 static irqreturn_t tegra_bb_mem_req_soon(int irq, void *data)
803 {
804         struct tegra_bb *bb = (struct tegra_bb *)data;
805         void __iomem *fctrl = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
806         u32 fc_sts;
807
808         spin_lock(&bb->lock);
809         /* clear the interrupt */
810         fc_sts = readl(fctrl + FLOW_CTLR_IPC_FLOW_IPC_STS_0);
811         if (fc_sts &
812                 (0x8 << FLOW_CTLR_IPC_FLOW_IPC_STS_0_AP2BB_MSC_STS_SHIFT)) {
813
814                 writel((0x8 << FLOW_CTLR_IPC_FLOW_IPC_STS_0_AP2BB_MSC_STS_SHIFT)
815                                 , fctrl + FLOW_CTLR_IPC_FLOW_IPC_CLR_0);
816
817                 irq_set_irq_type(bb->mem_req_soon, IRQF_TRIGGER_RISING);
818                 bb->state = BBC_SET_FLOOR;
819                 queue_work(bb->workqueue, &bb->work);
820         }
821         spin_unlock(&bb->lock);
822
823         return IRQ_HANDLED;
824 }
825
826 static inline void pmc_32kwritel(u32 val, unsigned long offs)
827 {
828         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
829         writel(val, pmc + offs);
830         udelay(130);
831 }
832
833 static void tegra_bb_enable_pmc_wake(void)
834 {
835         /* Set PMC wake interrupt on active low
836          * please see Bug 1181348 for details of SW WAR
837          */
838         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
839         u32 reg = readl(pmc + PMC_CTRL2);
840         reg &= ~(PMC_CTRL2_WAKE_DET_EN);
841         pmc_32kwritel(reg, PMC_CTRL2);
842
843         reg = readl(pmc + PMC_WAKE2_LEVEL);
844         reg &= ~(PMC_WAKE2_BB_MEM_REQ);
845         pmc_32kwritel(reg, PMC_WAKE2_LEVEL);
846
847         usleep_range(1000, 1100);
848         pmc_32kwritel(1, PMC_AUTO_WAKE_LVL);
849
850         usleep_range(1000, 1100);
851         reg = readl(pmc + PMC_WAKE2_MASK);
852         reg |= PMC_WAKE2_BB_MEM_REQ;
853         pmc_32kwritel(reg, PMC_WAKE2_MASK);
854
855         reg = readl(pmc + PMC_CTRL2);
856         reg |= PMC_CTRL2_WAKE_DET_EN;
857         pmc_32kwritel(reg, PMC_CTRL2);
858
859         pr_debug("%s\n", __func__);
860 }
861
862 static irqreturn_t tegra_pmc_wake_intr(int irq, void *data)
863 {
864         struct tegra_bb *bb = (struct tegra_bb *)data;
865         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
866         static void __iomem *tert_ictlr = IO_ADDRESS(TEGRA_TERTIARY_ICTLR_BASE);
867         u32 lic, sts;
868         int mem_req;
869
870         spin_lock(&bb->lock);
871         sts = readl(pmc + APBDEV_PMC_IPC_PMC_IPC_STS_0);
872         mem_req = (sts >> APBDEV_PMC_IPC_PMC_IPC_STS_0_BB2AP_MEM_REQ_SHIFT) & 1;
873
874         /* clear interrupt */
875         lic = readl(tert_ictlr + TRI_ICTLR_VIRQ_CPU);
876         if (lic & TRI_ICTLR_PMC_WAKE_INT) {
877                 pr_debug("clear pmc_wake sts %x mem_req %d\n", sts, mem_req);
878                 writel(TRI_ICTLR_PMC_WAKE_INT,
879                                 tert_ictlr + TRI_ICTLR_CPU_IER_CLR);
880         }
881
882         irq_set_irq_type(INT_PMC_WAKE_INT, IRQF_TRIGGER_RISING);
883         if (!mem_req) {
884
885                 bb->state = BBC_REMOVE_FLOOR;
886                 queue_work(bb->workqueue, &bb->work);
887         }
888         spin_unlock(&bb->lock);
889
890         return IRQ_HANDLED;
891 }
892
893 static void tegra_bb_emc_dvfs(struct work_struct *work)
894 {
895         struct tegra_bb *bb = container_of(work, struct tegra_bb, work);
896         unsigned long flags;
897
898         if (!bb)
899                 return;
900
901         spin_lock_irqsave(&bb->lock, flags);
902         if (bb->prev_state == bb->state) {
903                 spin_unlock_irqrestore(&bb->lock, flags);
904                 return;
905         }
906
907         switch (bb->state) {
908         case BBC_SET_FLOOR:
909                 bb->prev_state = bb->state;
910                 spin_unlock_irqrestore(&bb->lock, flags);
911
912                 pm_runtime_get_sync(bb->dev);
913                 /* going from 0 to high */
914                 clk_prepare_enable(bb->emc_clk);
915                 if (bb->emc_flags & EMC_DSR)
916                         tegra_emc_dsr_override(TEGRA_EMC_DSR_OVERRIDE);
917                 clk_set_rate(bb->emc_clk, bb->emc_min_freq);
918                 pr_debug("bbc setting floor to %lu\nMHz",
919                                                 bb->emc_min_freq/1000000);
920
921                 /* restore iso bw request*/
922                 tegra_bbc_proxy_restore_iso(bb->proxy_dev);
923
924                 /* reenable pmc_wake_det irq */
925                 tegra_bb_enable_pmc_wake();
926                 irq_set_irq_type(INT_PMC_WAKE_INT, IRQF_TRIGGER_HIGH);
927                 return;
928         case BBC_REMOVE_FLOOR:
929                 /* discard erroneous request */
930                 if (bb->prev_state != BBC_SET_FLOOR) {
931                         spin_unlock_irqrestore(&bb->lock, flags);
932                         return;
933                 }
934                 bb->prev_state = bb->state;
935                 spin_unlock_irqrestore(&bb->lock, flags);
936
937                 /* remove iso bandwitdh request from bbc */
938                 tegra_bbc_proxy_clear_iso(bb->proxy_dev);
939
940                 /* going from high to 0 */
941                 if (bb->emc_flags & EMC_DSR)
942                         tegra_emc_dsr_override(TEGRA_EMC_DSR_NORMAL);
943                 clk_set_rate(bb->emc_clk, 0);
944                 clk_disable_unprepare(bb->emc_clk);
945                 pr_debug("bbc removing emc floor\n");
946
947                 /* reenable mem_req_soon irq */
948                 tegra_bb_enable_mem_req_soon();
949                 irq_set_irq_type(bb->mem_req_soon, IRQF_TRIGGER_HIGH);
950                 pm_runtime_put(bb->dev);
951                 return;
952
953         case BBC_CRASHDUMP_FLOOR:
954                 /* BBC is crashed and ready to send coredump.
955                  * do not store prev_state */
956                 spin_unlock_irqrestore(&bb->lock, flags);
957
958                 pr_info("%s: bbc crash detected, set EMC to max\n", __func__);
959                 if (bb->prev_state != BBC_SET_FLOOR) {
960                         pm_runtime_get_sync(bb->dev);
961                         clk_prepare_enable(bb->emc_clk);
962                 }
963
964                 tegra_emc_dsr_override(TEGRA_EMC_DSR_OVERRIDE);
965                 clk_set_rate(bb->emc_clk, BBC_MC_MAX_FREQ);
966                 return;
967         default:
968                 spin_unlock_irqrestore(&bb->lock, flags);
969                 break;
970         }
971
972         return;
973 }
974
975 void tegra_bb_set_emc_floor(struct device *dev, unsigned long freq, u32 flags)
976 {
977         struct platform_device *pdev = container_of(dev, struct platform_device,
978                                                     dev);
979         struct tegra_bb_platform_data *pdata;
980         struct tegra_bb *bb;
981
982         if (!pdev) {
983                 dev_err(dev, "%s platform device is NULL\n", __func__);
984                 return;
985         }
986
987         pdata = pdev->dev.platform_data;
988         if (!pdata) {
989                 dev_err(&pdev->dev, "%s platform data not found\n", __func__);
990                 return;
991         }
992
993         bb = (struct tegra_bb *)pdata->bb_handle;
994         if (!bb) {
995                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
996                 return;
997         }
998
999         if (bb->emc_min_freq != freq) {
1000                 bb->emc_min_freq = freq;
1001                 clk_set_rate(bb->emc_clk, bb->emc_min_freq);
1002         }
1003
1004         bb->emc_flags = flags;
1005         return;
1006 }
1007 EXPORT_SYMBOL(tegra_bb_set_emc_floor);
1008
1009 #endif
1010
1011 static int tegra_bb_probe(struct platform_device *pdev)
1012 {
1013         struct tegra_bb *bb;
1014         int ret;
1015         struct tegra_bb_platform_data *pdata;
1016         void __iomem *tegra_mc = IO_ADDRESS(TEGRA_MC_BASE);
1017         unsigned int size, bbc_mem_regions_0;
1018         struct clk *c;
1019         unsigned int mb_size = SZ_4K + SZ_128K; /* config + stats */
1020
1021         if (!pdev) {
1022                 pr_err("%s platform device is NULL!\n", __func__);
1023                 return -EINVAL;
1024         }
1025
1026         pdata = pdev->dev.platform_data;
1027
1028         if (!pdata) {
1029                 dev_err(&pdev->dev, "%s tegra_bb not found!\n", __func__);
1030                 return -EINVAL;
1031         }
1032
1033         dev_dbg(&pdev->dev, "%s\n", __func__);
1034         bb =  kzalloc(sizeof(struct tegra_bb), GFP_KERNEL);
1035
1036         if (bb == NULL) {
1037                 kfree(bb);
1038                 return -ENOMEM;
1039         }
1040
1041         spin_lock_init(&bb->lock);
1042
1043         /* Private region */
1044         bbc_mem_regions_0 = readl(tegra_mc + MC_BBC_MEM_REGIONS_0_OFFSET);
1045
1046         pr_info("%s MC_BBC_MEM_REGIONS_0=0x%x\n", __func__, bbc_mem_regions_0);
1047
1048         size = (bbc_mem_regions_0 >> MC_BBC_MEM_REGIONS_0_PRIV_SIZE_SHIFT) &
1049                 MC_BBC_MEM_REGIONS_0_PRIV_SIZE_MASK;
1050
1051         /* Private */
1052         switch (size) {
1053         case 0:
1054                 bb->priv_size = SZ_8M;
1055                 break;
1056         case 1:
1057                 bb->priv_size = SZ_16M;
1058                 break;
1059         case 2:
1060                 bb->priv_size = SZ_32M;
1061                 break;
1062         case 3:
1063                 bb->priv_size = SZ_64M;
1064                 break;
1065         case 7:
1066                 pr_err("%s no private memory mapped\n", __func__);
1067                 break;
1068         default:
1069                 pr_err("%s invalid private memory size 0x%x\n", __func__, size);
1070                 break;
1071         }
1072
1073         bb->priv_phy =
1074                 ((bbc_mem_regions_0 >> MC_BBC_MEM_REGIONS_0_PRIV_BASE_SHIFT)
1075                  & MC_BBC_MEM_REGIONS_0_PRIV_BASE_MASK) << 23;
1076
1077         /* IPC */
1078         size = (bbc_mem_regions_0 >> MC_BBC_MEM_REGIONS_0_IPC_SIZE_SHIFT) &
1079                 MC_BBC_MEM_REGIONS_0_IPC_SIZE_MASK;
1080
1081         switch (size) {
1082         case 0:
1083                 bb->ipc_size = SZ_8M;
1084                 break;
1085         case 1:
1086                 bb->ipc_size = SZ_16M;
1087                 break;
1088         case 2:
1089                 bb->ipc_size = SZ_32M;
1090                 break;
1091         case 3:
1092                 bb->ipc_size = SZ_64M;
1093                 break;
1094         case 7:
1095                 pr_err("%s no IPC memory mapped\n", __func__);
1096                 break;
1097         default:
1098                 pr_err("%s invalid IPC  memory size 0x%x\n", __func__, size);
1099                 break;
1100         }
1101
1102         bb->ipc_phy =
1103                 ((bbc_mem_regions_0 >> MC_BBC_MEM_REGIONS_0_IPC_BASE_SHIFT)
1104                  & MC_BBC_MEM_REGIONS_0_IPC_BASE_MASK) << 23;
1105
1106         pr_info("%s  priv@0x%lx/0x%lx\n", __func__,
1107                  (unsigned long)bb->priv_phy,
1108                 bb->priv_size);
1109
1110         pr_info("%s  ipc@0x%lx/0x%lx\n", __func__,
1111                  (unsigned long)bb->ipc_phy,
1112                 bb->ipc_size);
1113
1114         if (!(bb->ipc_size && bb->priv_size)) {
1115                 pr_err("%s: invalid tegra_bb regions\n", __func__);
1116                 BUG();
1117         }
1118
1119         bb->irq = pdata->bb_irq;
1120         bb->mem_req_soon = pdata->mem_req_soon;
1121
1122         pdata->bb_handle = bb;
1123
1124         /* Map mb_virt uncached (first 132K of IPC for config + statistics) */
1125         bb->mb_virt = ioremap_nocache(bb->ipc_phy, mb_size);
1126         pr_debug("%s: uncached IPC Virtual=0x%p\n", __func__, bb->mb_virt);
1127
1128         /* IPC memory is cached */
1129         bb->ipc_virt =  ioremap_cached(bb->ipc_phy, bb->ipc_size);
1130         pr_debug("%s: IPC Virtual=0x%p\n", __func__, bb->ipc_virt);
1131
1132         /* clear the first 4K of IPC memory */
1133         memset(bb->mb_virt, 0, SZ_1K*4);
1134
1135         /* init value of cold boot */
1136         *(unsigned int *)bb->mb_virt = TEGRA_BB_IPC_COLDBOOT |
1137                 ((~TEGRA_BB_IPC_COLDBOOT) << 16);
1138
1139         /* Register devs */
1140         bb->dev_priv.minor = MISC_DYNAMIC_MINOR;
1141         snprintf(bb->priv_name, sizeof(bb->priv_name),
1142                  "tegra_bb_priv%d", pdev->id);
1143
1144         bb->dev_priv.name = bb->priv_name;
1145         bb->dev_priv.fops = &tegra_bb_priv_fops;
1146         bb->dev_priv.parent = &pdev->dev;
1147
1148         bb->dev_ipc.minor = MISC_DYNAMIC_MINOR;
1149         snprintf(bb->ipc_name, sizeof(bb->ipc_name),
1150                  "tegra_bb_ipc%d",
1151                  pdev->id);
1152
1153         bb->dev_ipc.name = bb->ipc_name;
1154         bb->dev_ipc.fops = &tegra_bb_ipc_fops;
1155         bb->dev_ipc.parent = &pdev->dev;
1156
1157         ret = misc_register(&bb->dev_priv);
1158         if (ret) {
1159                 dev_err(&pdev->dev, "unable to register miscdevice %s\n",
1160                         bb->dev_priv.name);
1161                 kfree(bb);
1162                 return -EAGAIN;
1163         }
1164
1165         ret = misc_register(&bb->dev_ipc);
1166         if (ret) {
1167                 dev_err(&pdev->dev, "unable to register miscdevice %s\n",
1168                         bb->dev_ipc.name);
1169                 kfree(bb);
1170                 return -EAGAIN;
1171         }
1172
1173         bb->dev = &pdev->dev;
1174         bb->instance = pdev->id;
1175         bb->status = 0;
1176
1177         ret = device_create_file(&pdev->dev, &dev_attr_status);
1178         ret = device_create_file(&pdev->dev, &dev_attr_retcode);
1179         ret = device_create_file(&pdev->dev, &dev_attr_priv_size);
1180         ret = device_create_file(&pdev->dev, &dev_attr_ipc_size);
1181         ret = device_create_file(&pdev->dev, &dev_attr_reset);
1182         ret = device_create_file(&pdev->dev, &dev_attr_state);
1183
1184         bb->sd = sysfs_get_dirent(pdev->dev.kobj.sd, NULL, "status");
1185
1186         bb->vdd_bb_core = regulator_get(NULL, "vdd_bb");
1187         if (IS_ERR_OR_NULL(bb->vdd_bb_core)) {
1188                 pr_err("vdd_bb regulator get failed\n");
1189                 bb->vdd_bb_core = NULL;
1190         }
1191
1192         bb->vdd_bb_pll = regulator_get(NULL, "avdd_bb_pll");
1193         if (IS_ERR_OR_NULL(bb->vdd_bb_pll)) {
1194                 pr_err("avdd_bb_pll regulator get failed\n");
1195                 bb->vdd_bb_pll = NULL;
1196         }
1197
1198         /* clk enable for mc_bbc / pll_p_bbc */
1199         c = tegra_get_clock_by_name("mc_bbc");
1200         if (IS_ERR_OR_NULL(c))
1201                 pr_err("mc_bbc get failed\n");
1202         else
1203                 clk_enable(c);
1204         c = tegra_get_clock_by_name("pll_p_bbc");
1205         if (IS_ERR_OR_NULL(c))
1206                 pr_err("pll_p_bbc get failed\n");
1207         else
1208                 clk_enable(c);
1209
1210         bb->nvshm_pdata.ipc_base_virt = bb->ipc_virt;
1211         bb->nvshm_pdata.ipc_size = bb->ipc_size;
1212         bb->nvshm_pdata.mb_base_virt = bb->mb_virt;
1213         bb->nvshm_pdata.mb_size = mb_size;
1214         bb->nvshm_pdata.bb_irq = bb->irq;
1215         bb->nvshm_pdata.tegra_bb = pdev;
1216         bb->nvshm_device.name = "nvshm";
1217         bb->nvshm_device.id = bb->instance;
1218         bb->nvshm_device.dev.platform_data = &bb->nvshm_pdata;
1219         platform_device_register(&bb->nvshm_device);
1220         platform_set_drvdata(pdev, bb);
1221
1222         tegra_pd_add_device(&pdev->dev);
1223         pm_runtime_enable(&pdev->dev);
1224
1225 #ifndef CONFIG_TEGRA_BASEBAND_SIMU
1226         snprintf(bb->name, sizeof(bb->name), "tegra_bb%d", pdev->id);
1227
1228         ret = request_irq(bb->irq, tegra_bb_isr_handler, IRQF_TRIGGER_HIGH,
1229                                 bb->name, bb);
1230         if (ret) {
1231                 dev_err(&pdev->dev, "Could not register irq handler\n");
1232                 kfree(bb);
1233                 return -EAGAIN;
1234         }
1235
1236         ret = enable_irq_wake(bb->irq);
1237         if (ret) {
1238                 dev_err(&pdev->dev, "set enable_irq_wake failed\n");
1239                 kfree(bb);
1240                 return -EAGAIN;
1241         }
1242
1243         /* setup emc dvfs request framework */
1244         bb->emc_clk = clk_get(&pdev->dev, "emc_fl");
1245         if (IS_ERR(bb->emc_clk)) {
1246                 dev_err(&pdev->dev, "can't get emc clock\n");
1247                 kfree(bb);
1248                 return -ENOENT;
1249         }
1250
1251         bb->workqueue = alloc_workqueue("bbc-pm", WQ_HIGHPRI, 1);
1252         if (!bb->workqueue) {
1253                 dev_err(&pdev->dev, "failed to create workqueue\n");
1254                 kfree(bb);
1255                 return -ENOMEM;
1256         }
1257         INIT_WORK(&bb->work, tegra_bb_emc_dvfs);
1258
1259         bb->emc_min_freq = BBC_MC_MIN_FREQ;
1260         bb->emc_flags = EMC_DSR;
1261
1262         /* get bbc proxy device struct, it should be registered
1263          * before this driver.
1264          */
1265         bb->proxy_dev = bus_find_device_by_name(&platform_bus_type,  NULL,
1266                                 "tegra_bbc_proxy");
1267         if (!bb->proxy_dev)
1268                 dev_warn(&pdev->dev, "%s: bbc proxy device not found!\n",
1269                                 __func__);
1270
1271         ret = request_irq(bb->mem_req_soon, tegra_bb_mem_req_soon,
1272                         IRQF_TRIGGER_RISING, "bb_mem_req_soon", bb);
1273         if (ret) {
1274                 dev_err(&pdev->dev, "Could not register mem_req_soon irq\n");
1275                 kfree(bb);
1276                 return -EAGAIN;
1277         }
1278         tegra_bb_enable_mem_req_soon();
1279
1280         ret = request_irq(INT_PMC_WAKE_INT, tegra_pmc_wake_intr,
1281                         IRQF_TRIGGER_RISING, "tegra_pmc_wake_intr", bb);
1282         if (ret) {
1283                 dev_err(&pdev->dev,
1284                         "Could not register pmc_wake_int irq handler\n");
1285                 kfree(bb);
1286                 return -EAGAIN;
1287         }
1288
1289 #endif
1290         return 0;
1291 }
1292
1293 #ifdef CONFIG_PM
1294 static int tegra_bb_suspend(struct platform_device *pdev, pm_message_t state)
1295 {
1296         void __iomem *fctrl = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
1297         int sts;
1298
1299         dev_dbg(&pdev->dev, "%s\n", __func__);
1300         /* Be sure to ack any pending irq from BB */
1301
1302         sts = readl(fctrl + FLOW_CTLR_IPC_FLOW_IPC_STS_0);
1303         if (sts & (1 << FLOW_CTLR_IPC_FLOW_IPC_STS_0_BB2AP_INT0_STS_SHIFT)) {
1304                 writel(1 << FLOW_CTLR_IPC_FLOW_IPC_CLR_0_BB2AP_INT0_STS_SHIFT,
1305                        fctrl + FLOW_CTLR_IPC_FLOW_IPC_CLR_0);
1306         }
1307         return 0;
1308 }
1309
1310 static int tegra_bb_resume(struct platform_device *pdev)
1311 {
1312 #ifndef CONFIG_TEGRA_BASEBAND_SIMU
1313         struct tegra_bb *bb;
1314         struct tegra_bb_platform_data *pdata;
1315 #endif
1316         if (!pdev) {
1317                 pr_err("%s platform device is NULL!\n", __func__);
1318                 return -EINVAL;
1319         }
1320         dev_dbg(&pdev->dev, "%s\n", __func__);
1321
1322 #ifndef CONFIG_TEGRA_BASEBAND_SIMU
1323         pdata = pdev->dev.platform_data;
1324         if (!pdata) {
1325                 dev_err(&pdev->dev, "%s platform dev not found!\n", __func__);
1326                 return -EINVAL;
1327         }
1328         bb = (struct tegra_bb *)pdata->bb_handle;
1329
1330         tegra_bb_enable_mem_req_soon();
1331         irq_set_irq_type(bb->mem_req_soon, IRQF_TRIGGER_HIGH);
1332
1333         tegra_bb_enable_pmc_wake();
1334         irq_set_irq_type(INT_PMC_WAKE_INT, IRQF_TRIGGER_HIGH);
1335 #endif
1336         return 0;
1337 }
1338 #endif
1339
1340 static struct platform_driver tegra_bb_driver = {
1341         .driver = {
1342                 .name = "tegra_bb",
1343                 .owner = THIS_MODULE,
1344         },
1345         .probe = tegra_bb_probe,
1346 #ifdef CONFIG_PM
1347         .suspend = tegra_bb_suspend,
1348         .resume = tegra_bb_resume,
1349 #endif
1350 };
1351
1352 static int __init tegra_bb_init(void)
1353 {
1354         int ret;
1355         ret = platform_driver_register(&tegra_bb_driver);
1356         pr_debug("%s ret %d\n", __func__, ret);
1357         return ret;
1358 }
1359
1360 static void __exit tegra_bb_exit(void)
1361 {
1362         pr_debug("%s\n", __func__);
1363         platform_driver_unregister(&tegra_bb_driver);
1364 }
1365
1366 fs_initcall(tegra_bb_init);
1367 module_exit(tegra_bb_exit);
1368
1369 #ifdef CONFIG_DEBUG_FS
1370 static struct dentry *bb_debugfs_root;
1371 static enum pmc_event_sel {
1372         PMC_EVENT_NONE,
1373         PMC_EVENT_LP0BB,
1374         PMC_EVENT_LP0ACTIVE,
1375 } pmc_event_sel;
1376
1377 static int lp0bb_transitions_set(void *data, u64 val)
1378 {
1379         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
1380         u32 reg;
1381
1382         if (!val) {
1383                 /* disable event counting and clear counter */
1384                 reg = 0;
1385                 writel(reg, pmc + APBDEV_PMC_EVENT_COUNTER_0);
1386                 pmc_event_sel = PMC_EVENT_NONE;
1387         } else if (val == 1) {
1388                 reg = APBDEV_PMC_EVENT_COUNTER_0_EN_MASK |
1389                                 APBDEV_PMC_EVENT_COUNTER_0_LP0BB;
1390                  /* lp0->lp0bb transitions */
1391                 writel(reg, pmc + APBDEV_PMC_EVENT_COUNTER_0);
1392                 pmc_event_sel = PMC_EVENT_LP0BB;
1393         }
1394         return 0;
1395 }
1396
1397 static inline unsigned long read_pmc_event_counter(void)
1398 {
1399         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
1400         u32 reg = readl(pmc + APBDEV_PMC_EVENT_COUNTER_0);
1401         /* hw event counter is 16 bit */
1402         reg = reg & 0xffff;
1403         return reg;
1404 }
1405
1406 static int lp0bb_transitions_get(void *data, u64 *val)
1407 {
1408         if (pmc_event_sel == PMC_EVENT_LP0BB)
1409                 *val = (u32) read_pmc_event_counter();
1410         else
1411                 *val = 0;
1412         return 0;
1413 }
1414 DEFINE_SIMPLE_ATTRIBUTE(lp0bb_fops, lp0bb_transitions_get,
1415                 lp0bb_transitions_set, "%lld\n");
1416
1417 static int lp0active_transitions_set(void *data, u64 val)
1418 {
1419         void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
1420         u32 reg;
1421
1422         if (!val) {
1423                 /* disable event counting and clear counter */
1424                 reg = 0;
1425                 writel(reg, pmc + APBDEV_PMC_EVENT_COUNTER_0);
1426                 pmc_event_sel = PMC_EVENT_NONE;
1427         } else if (val == 1) {
1428                 reg = APBDEV_PMC_EVENT_COUNTER_0_EN_MASK |
1429                                 APBDEV_PMC_EVENT_COUNTER_0_LP0ACTIVE;
1430                  /* lp0->active transitions */
1431                 writel(reg, pmc + APBDEV_PMC_EVENT_COUNTER_0);
1432                 pmc_event_sel = PMC_EVENT_LP0ACTIVE;
1433         }
1434         return 0;
1435 }
1436 static int lp0active_transitions_get(void *data, u64 *val)
1437 {
1438         if (pmc_event_sel == PMC_EVENT_LP0ACTIVE)
1439                 *val = (u32) read_pmc_event_counter();
1440         else
1441                 *val = 0;
1442         return 0;
1443 }
1444 DEFINE_SIMPLE_ATTRIBUTE(lp0active_fops, lp0active_transitions_get,
1445                 lp0active_transitions_set, "%lld\n");
1446
1447 static int __init tegra_bb_debug_init(void)
1448 {
1449         struct dentry *d;
1450
1451         bb_debugfs_root = debugfs_create_dir("tegra_bb", NULL);
1452         if (!bb_debugfs_root)
1453                 return -ENOMEM;
1454
1455         d = debugfs_create_file("lp0bb_transitions", S_IWUSR | S_IRUGO,
1456                         bb_debugfs_root, NULL, &lp0bb_fops);
1457         if (!d)
1458                 goto err;
1459
1460         d = debugfs_create_file("lp0active_transitions", S_IWUSR | S_IRUGO,
1461                         bb_debugfs_root, NULL, &lp0active_fops);
1462         if (!d)
1463                 goto err;
1464
1465         return 0;
1466 err:
1467         debugfs_remove_recursive(bb_debugfs_root);
1468         return -ENOMEM;
1469 }
1470
1471 late_initcall(tegra_bb_debug_init);
1472 #endif
1473
1474 MODULE_DESCRIPTION("Tegra T148 BB Module");
1475 MODULE_LICENSE("GPL");