blob: 77bc8d4c1d5f39499f838d92feb69821be74500d [file] [log] [blame]
Michael Buesch61e115a2007-09-18 15:12:50 -04001/*
2 * Sonics Silicon Backplane
3 * Broadcom MIPS core driver
4 *
5 * Copyright 2005, Broadcom Corporation
Michael Büscheb032b92011-07-04 20:50:05 +02006 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
Michael Buesch61e115a2007-09-18 15:12:50 -04007 *
8 * Licensed under the GNU/GPL. See COPYING for details.
9 */
10
11#include <linux/ssb/ssb.h>
12
13#include <linux/serial.h>
14#include <linux/serial_core.h>
15#include <linux/serial_reg.h>
16#include <linux/time.h>
17
18#include "ssb_private.h"
19
20
21static inline u32 mips_read32(struct ssb_mipscore *mcore,
22 u16 offset)
23{
24 return ssb_read32(mcore->dev, offset);
25}
26
27static inline void mips_write32(struct ssb_mipscore *mcore,
28 u16 offset,
29 u32 value)
30{
31 ssb_write32(mcore->dev, offset, value);
32}
33
34static const u32 ipsflag_irq_mask[] = {
35 0,
36 SSB_IPSFLAG_IRQ1,
37 SSB_IPSFLAG_IRQ2,
38 SSB_IPSFLAG_IRQ3,
39 SSB_IPSFLAG_IRQ4,
40};
41
42static const u32 ipsflag_irq_shift[] = {
43 0,
44 SSB_IPSFLAG_IRQ1_SHIFT,
45 SSB_IPSFLAG_IRQ2_SHIFT,
46 SSB_IPSFLAG_IRQ3_SHIFT,
47 SSB_IPSFLAG_IRQ4_SHIFT,
48};
49
50static inline u32 ssb_irqflag(struct ssb_device *dev)
51{
matthieu castetea4bbfd2009-06-30 23:04:55 +020052 u32 tpsflag = ssb_read32(dev, SSB_TPSFLAG);
53 if (tpsflag)
54 return ssb_read32(dev, SSB_TPSFLAG) & SSB_TPSFLAG_BPFLAG;
55 else
56 /* not irq supported */
57 return 0x3f;
58}
59
60static struct ssb_device *find_device(struct ssb_device *rdev, int irqflag)
61{
62 struct ssb_bus *bus = rdev->bus;
63 int i;
64 for (i = 0; i < bus->nr_devices; i++) {
65 struct ssb_device *dev;
66 dev = &(bus->devices[i]);
67 if (ssb_irqflag(dev) == irqflag)
68 return dev;
69 }
70 return NULL;
Michael Buesch61e115a2007-09-18 15:12:50 -040071}
72
73/* Get the MIPS IRQ assignment for a specified device.
74 * If unassigned, 0 is returned.
matthieu castetea4bbfd2009-06-30 23:04:55 +020075 * If disabled, 5 is returned.
76 * If not supported, 6 is returned.
Michael Buesch61e115a2007-09-18 15:12:50 -040077 */
78unsigned int ssb_mips_irq(struct ssb_device *dev)
79{
80 struct ssb_bus *bus = dev->bus;
matthieu castetea4bbfd2009-06-30 23:04:55 +020081 struct ssb_device *mdev = bus->mipscore.dev;
Michael Buesch61e115a2007-09-18 15:12:50 -040082 u32 irqflag;
83 u32 ipsflag;
84 u32 tmp;
85 unsigned int irq;
86
87 irqflag = ssb_irqflag(dev);
matthieu castetea4bbfd2009-06-30 23:04:55 +020088 if (irqflag == 0x3f)
89 return 6;
Michael Buesch61e115a2007-09-18 15:12:50 -040090 ipsflag = ssb_read32(bus->mipscore.dev, SSB_IPSFLAG);
91 for (irq = 1; irq <= 4; irq++) {
92 tmp = ((ipsflag & ipsflag_irq_mask[irq]) >> ipsflag_irq_shift[irq]);
93 if (tmp == irqflag)
94 break;
95 }
matthieu castetea4bbfd2009-06-30 23:04:55 +020096 if (irq == 5) {
97 if ((1 << irqflag) & ssb_read32(mdev, SSB_INTVEC))
98 irq = 0;
99 }
Michael Buesch61e115a2007-09-18 15:12:50 -0400100
101 return irq;
102}
103
104static void clear_irq(struct ssb_bus *bus, unsigned int irq)
105{
106 struct ssb_device *dev = bus->mipscore.dev;
107
108 /* Clear the IRQ in the MIPScore backplane registers */
109 if (irq == 0) {
110 ssb_write32(dev, SSB_INTVEC, 0);
111 } else {
112 ssb_write32(dev, SSB_IPSFLAG,
113 ssb_read32(dev, SSB_IPSFLAG) |
114 ipsflag_irq_mask[irq]);
115 }
116}
117
118static void set_irq(struct ssb_device *dev, unsigned int irq)
119{
120 unsigned int oldirq = ssb_mips_irq(dev);
121 struct ssb_bus *bus = dev->bus;
122 struct ssb_device *mdev = bus->mipscore.dev;
123 u32 irqflag = ssb_irqflag(dev);
124
matthieu castetea4bbfd2009-06-30 23:04:55 +0200125 BUG_ON(oldirq == 6);
126
Michael Buesch61e115a2007-09-18 15:12:50 -0400127 dev->irq = irq + 2;
128
Michael Buesch61e115a2007-09-18 15:12:50 -0400129 /* clear the old irq */
130 if (oldirq == 0)
131 ssb_write32(mdev, SSB_INTVEC, (~(1 << irqflag) & ssb_read32(mdev, SSB_INTVEC)));
matthieu castetea4bbfd2009-06-30 23:04:55 +0200132 else if (oldirq != 5)
Michael Buesch61e115a2007-09-18 15:12:50 -0400133 clear_irq(bus, oldirq);
134
135 /* assign the new one */
Michael Buesch2633da22008-04-08 11:17:29 +0200136 if (irq == 0) {
137 ssb_write32(mdev, SSB_INTVEC, ((1 << irqflag) | ssb_read32(mdev, SSB_INTVEC)));
138 } else {
matthieu castetea4bbfd2009-06-30 23:04:55 +0200139 u32 ipsflag = ssb_read32(mdev, SSB_IPSFLAG);
140 if ((ipsflag & ipsflag_irq_mask[irq]) != ipsflag_irq_mask[irq]) {
141 u32 oldipsflag = (ipsflag & ipsflag_irq_mask[irq]) >> ipsflag_irq_shift[irq];
142 struct ssb_device *olddev = find_device(dev, oldipsflag);
143 if (olddev)
144 set_irq(olddev, 0);
145 }
Michael Buesch2633da22008-04-08 11:17:29 +0200146 irqflag <<= ipsflag_irq_shift[irq];
matthieu castetea4bbfd2009-06-30 23:04:55 +0200147 irqflag |= (ipsflag & ~ipsflag_irq_mask[irq]);
Michael Buesch2633da22008-04-08 11:17:29 +0200148 ssb_write32(mdev, SSB_IPSFLAG, irqflag);
149 }
matthieu castetea4bbfd2009-06-30 23:04:55 +0200150 ssb_dprintk(KERN_INFO PFX
151 "set_irq: core 0x%04x, irq %d => %d\n",
152 dev->id.coreid, oldirq+2, irq+2);
153}
154
155static void print_irq(struct ssb_device *dev, unsigned int irq)
156{
157 int i;
158 static const char *irq_name[] = {"2(S)", "3", "4", "5", "6", "D", "I"};
159 ssb_dprintk(KERN_INFO PFX
160 "core 0x%04x, irq :", dev->id.coreid);
161 for (i = 0; i <= 6; i++) {
162 ssb_dprintk(" %s%s", irq_name[i], i==irq?"*":" ");
163 }
164 ssb_dprintk("\n");
165}
166
167static void dump_irq(struct ssb_bus *bus)
168{
169 int i;
170 for (i = 0; i < bus->nr_devices; i++) {
171 struct ssb_device *dev;
172 dev = &(bus->devices[i]);
173 print_irq(dev, ssb_mips_irq(dev));
174 }
Michael Buesch61e115a2007-09-18 15:12:50 -0400175}
176
177static void ssb_mips_serial_init(struct ssb_mipscore *mcore)
178{
179 struct ssb_bus *bus = mcore->dev->bus;
180
Hauke Mehrtens03620632012-11-27 00:31:55 +0100181 if (ssb_extif_available(&bus->extif))
Michael Buesch61e115a2007-09-18 15:12:50 -0400182 mcore->nr_serial_ports = ssb_extif_serial_init(&bus->extif, mcore->serial_ports);
Hauke Mehrtens03620632012-11-27 00:31:55 +0100183 else if (ssb_chipco_available(&bus->chipco))
Michael Buesch61e115a2007-09-18 15:12:50 -0400184 mcore->nr_serial_ports = ssb_chipco_serial_init(&bus->chipco, mcore->serial_ports);
185 else
186 mcore->nr_serial_ports = 0;
187}
188
189static void ssb_mips_flash_detect(struct ssb_mipscore *mcore)
190{
191 struct ssb_bus *bus = mcore->dev->bus;
Rafał Miłeckif1ab57e2013-01-25 11:36:25 +0100192 struct ssb_pflash *pflash = &mcore->pflash;
Michael Buesch61e115a2007-09-18 15:12:50 -0400193
Rafał Miłecki902d9e02012-08-08 19:37:04 +0200194 /* When there is no chipcommon on the bus there is 4MB flash */
Hauke Mehrtens03620632012-11-27 00:31:55 +0100195 if (!ssb_chipco_available(&bus->chipco)) {
Rafał Miłeckif1ab57e2013-01-25 11:36:25 +0100196 pflash->present = true;
197 pflash->buswidth = 2;
198 pflash->window = SSB_FLASH1;
199 pflash->window_size = SSB_FLASH1_SZ;
Rafał Miłecki902d9e02012-08-08 19:37:04 +0200200 return;
201 }
202
203 /* There is ChipCommon, so use it to read info about flash */
204 switch (bus->chipco.capabilities & SSB_CHIPCO_CAP_FLASHT) {
205 case SSB_CHIPCO_FLASHT_STSER:
206 case SSB_CHIPCO_FLASHT_ATSER:
Rafał Miłecki72a525c2013-01-06 21:48:50 +0100207 pr_debug("Found serial flash\n");
208 ssb_sflash_init(&bus->chipco);
Rafał Miłecki902d9e02012-08-08 19:37:04 +0200209 break;
210 case SSB_CHIPCO_FLASHT_PARA:
211 pr_debug("Found parallel flash\n");
Rafał Miłeckif1ab57e2013-01-25 11:36:25 +0100212 pflash->present = true;
213 pflash->window = SSB_FLASH2;
214 pflash->window_size = SSB_FLASH2_SZ;
Michael Buesch61e115a2007-09-18 15:12:50 -0400215 if ((ssb_read32(bus->chipco.dev, SSB_CHIPCO_FLASH_CFG)
216 & SSB_CHIPCO_CFG_DS16) == 0)
Rafał Miłeckif1ab57e2013-01-25 11:36:25 +0100217 pflash->buswidth = 1;
Rafał Miłecki902d9e02012-08-08 19:37:04 +0200218 else
Rafał Miłeckif1ab57e2013-01-25 11:36:25 +0100219 pflash->buswidth = 2;
Rafał Miłecki902d9e02012-08-08 19:37:04 +0200220 break;
Michael Buesch61e115a2007-09-18 15:12:50 -0400221 }
222}
223
224u32 ssb_cpu_clock(struct ssb_mipscore *mcore)
225{
226 struct ssb_bus *bus = mcore->dev->bus;
227 u32 pll_type, n, m, rate = 0;
228
Hauke Mehrtensd486a5b2012-02-01 00:13:56 +0100229 if (bus->chipco.capabilities & SSB_CHIPCO_CAP_PMU)
230 return ssb_pmu_get_cpu_clock(&bus->chipco);
231
Hauke Mehrtens03620632012-11-27 00:31:55 +0100232 if (ssb_extif_available(&bus->extif)) {
Michael Buesch61e115a2007-09-18 15:12:50 -0400233 ssb_extif_get_clockcontrol(&bus->extif, &pll_type, &n, &m);
Hauke Mehrtens03620632012-11-27 00:31:55 +0100234 } else if (ssb_chipco_available(&bus->chipco)) {
Michael Buesch61e115a2007-09-18 15:12:50 -0400235 ssb_chipco_get_clockcpu(&bus->chipco, &pll_type, &n, &m);
236 } else
237 return 0;
238
239 if ((pll_type == SSB_PLLTYPE_5) || (bus->chip_id == 0x5365)) {
240 rate = 200000000;
241 } else {
242 rate = ssb_calc_clock_rate(pll_type, n, m);
243 }
244
245 if (pll_type == SSB_PLLTYPE_6) {
246 rate *= 2;
247 }
248
249 return rate;
250}
251
252void ssb_mipscore_init(struct ssb_mipscore *mcore)
253{
Felix Fietkau7007d00ca2007-10-14 21:04:22 +0200254 struct ssb_bus *bus;
Michael Buesch61e115a2007-09-18 15:12:50 -0400255 struct ssb_device *dev;
256 unsigned long hz, ns;
257 unsigned int irq, i;
258
259 if (!mcore->dev)
260 return; /* We don't have a MIPS core */
261
262 ssb_dprintk(KERN_INFO PFX "Initializing MIPS core...\n");
263
Felix Fietkau7007d00ca2007-10-14 21:04:22 +0200264 bus = mcore->dev->bus;
Michael Buesch61e115a2007-09-18 15:12:50 -0400265 hz = ssb_clockspeed(bus);
266 if (!hz)
267 hz = 100000000;
268 ns = 1000000000 / hz;
269
Hauke Mehrtens03620632012-11-27 00:31:55 +0100270 if (ssb_extif_available(&bus->extif))
Michael Buesch61e115a2007-09-18 15:12:50 -0400271 ssb_extif_timing_init(&bus->extif, ns);
Hauke Mehrtens03620632012-11-27 00:31:55 +0100272 else if (ssb_chipco_available(&bus->chipco))
Michael Buesch61e115a2007-09-18 15:12:50 -0400273 ssb_chipco_timing_init(&bus->chipco, ns);
274
275 /* Assign IRQs to all cores on the bus, start with irq line 2, because serial usually takes 1 */
276 for (irq = 2, i = 0; i < bus->nr_devices; i++) {
matthieu castetea4bbfd2009-06-30 23:04:55 +0200277 int mips_irq;
Michael Buesch61e115a2007-09-18 15:12:50 -0400278 dev = &(bus->devices[i]);
matthieu castetea4bbfd2009-06-30 23:04:55 +0200279 mips_irq = ssb_mips_irq(dev);
280 if (mips_irq > 4)
281 dev->irq = 0;
282 else
283 dev->irq = mips_irq + 2;
284 if (dev->irq > 5)
285 continue;
Michael Buesch61e115a2007-09-18 15:12:50 -0400286 switch (dev->id.coreid) {
287 case SSB_DEV_USB11_HOST:
288 /* shouldn't need a separate irq line for non-4710, most of them have a proper
289 * external usb controller on the pci */
290 if ((bus->chip_id == 0x4710) && (irq <= 4)) {
291 set_irq(dev, irq++);
Michael Buesch61e115a2007-09-18 15:12:50 -0400292 }
matthieu castetea4bbfd2009-06-30 23:04:55 +0200293 break;
Michael Buesch61e115a2007-09-18 15:12:50 -0400294 case SSB_DEV_PCI:
295 case SSB_DEV_ETHERNET:
Michael Bueschaab547c2008-02-29 11:36:12 +0100296 case SSB_DEV_ETHERNET_GBIT:
Michael Buesch61e115a2007-09-18 15:12:50 -0400297 case SSB_DEV_80211:
298 case SSB_DEV_USB20_HOST:
299 /* These devices get their own IRQ line if available, the rest goes on IRQ0 */
300 if (irq <= 4) {
301 set_irq(dev, irq++);
302 break;
303 }
Jochen Friedrich83e34f02010-02-03 21:28:11 +0100304 /* fallthrough */
305 case SSB_DEV_EXTIF:
306 set_irq(dev, 0);
307 break;
Michael Buesch61e115a2007-09-18 15:12:50 -0400308 }
309 }
matthieu castetea4bbfd2009-06-30 23:04:55 +0200310 ssb_dprintk(KERN_INFO PFX "after irq reconfiguration\n");
311 dump_irq(bus);
Michael Buesch61e115a2007-09-18 15:12:50 -0400312
313 ssb_mips_serial_init(mcore);
314 ssb_mips_flash_detect(mcore);
315}