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