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