1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/types.h>
30#include <linux/pci.h>
31#include <linux/platform_device.h>
32#include <linux/kernel.h>
33#include <linux/init.h>
34#include <linux/dma-direct.h>
35#include <linux/mm.h>
36#include <linux/delay.h>
37#include <linux/bitops.h>
38#include <linux/irq.h>
39#include <linux/irqdomain.h>
40#include <linux/io.h>
41#include <asm/paccess.h>
42
43
44
45
46#define AR2315_PCI_1MS_REG 0x0008
47
48#define AR2315_PCI_1MS_MASK 0x3FFFF
49
50#define AR2315_PCI_MISC_CONFIG 0x000c
51
52#define AR2315_PCIMISC_TXD_EN 0x00000001
53#define AR2315_PCIMISC_CFG_SEL 0x00000002
54#define AR2315_PCIMISC_GIG_MASK 0x0000000C
55#define AR2315_PCIMISC_RST_MODE 0x00000030
56#define AR2315_PCIRST_INPUT 0x00000000
57#define AR2315_PCIRST_LOW 0x00000010
58#define AR2315_PCIRST_HIGH 0x00000020
59#define AR2315_PCIGRANT_EN 0x00000000
60#define AR2315_PCIGRANT_FRAME 0x00000040
61#define AR2315_PCIGRANT_IDLE 0x00000080
62#define AR2315_PCIGRANT_GAP 0x00000000
63#define AR2315_PCICACHE_DIS 0x00001000
64
65
66#define AR2315_PCI_OUT_TSTAMP 0x0010
67
68#define AR2315_PCI_UNCACHE_CFG 0x0014
69
70#define AR2315_PCI_IN_EN 0x0100
71
72#define AR2315_PCI_IN_EN0 0x01
73#define AR2315_PCI_IN_EN1 0x02
74#define AR2315_PCI_IN_EN2 0x04
75#define AR2315_PCI_IN_EN3 0x08
76
77#define AR2315_PCI_IN_DIS 0x0104
78
79#define AR2315_PCI_IN_DIS0 0x01
80#define AR2315_PCI_IN_DIS1 0x02
81#define AR2315_PCI_IN_DIS2 0x04
82#define AR2315_PCI_IN_DIS3 0x08
83
84#define AR2315_PCI_IN_PTR 0x0200
85
86#define AR2315_PCI_OUT_EN 0x0400
87
88#define AR2315_PCI_OUT_EN0 0x01
89
90#define AR2315_PCI_OUT_DIS 0x0404
91
92#define AR2315_PCI_OUT_DIS0 0x01
93
94#define AR2315_PCI_OUT_PTR 0x0408
95
96
97#define AR2315_PCI_ISR 0x0500
98
99#define AR2315_PCI_INT_TX 0x00000001
100#define AR2315_PCI_INT_TXOK 0x00000002
101#define AR2315_PCI_INT_TXERR 0x00000004
102#define AR2315_PCI_INT_TXEOL 0x00000008
103#define AR2315_PCI_INT_RX 0x00000010
104#define AR2315_PCI_INT_RXOK 0x00000020
105#define AR2315_PCI_INT_RXERR 0x00000040
106#define AR2315_PCI_INT_RXEOL 0x00000080
107#define AR2315_PCI_INT_TXOOD 0x00000200
108#define AR2315_PCI_INT_DESCMASK 0x0000FFFF
109#define AR2315_PCI_INT_EXT 0x02000000
110#define AR2315_PCI_INT_ABORT 0x04000000
111
112
113#define AR2315_PCI_IMR 0x0504
114
115
116#define AR2315_PCI_IER 0x0508
117
118#define AR2315_PCI_IER_DISABLE 0x00
119#define AR2315_PCI_IER_ENABLE 0x01
120
121#define AR2315_PCI_HOST_IN_EN 0x0800
122#define AR2315_PCI_HOST_IN_DIS 0x0804
123#define AR2315_PCI_HOST_IN_PTR 0x0810
124#define AR2315_PCI_HOST_OUT_EN 0x0900
125#define AR2315_PCI_HOST_OUT_DIS 0x0904
126#define AR2315_PCI_HOST_OUT_PTR 0x0908
127
128
129
130
131
132#define AR2315_PCI_IRQ_EXT 25
133#define AR2315_PCI_IRQ_ABORT 26
134#define AR2315_PCI_IRQ_COUNT 27
135
136
137#define AR2315_PCI_CFG_SIZE 0x00100000
138
139#define AR2315_PCI_HOST_SLOT 3
140#define AR2315_PCI_HOST_DEVID ((0xff18 << 16) | PCI_VENDOR_ID_ATHEROS)
141
142
143
144
145
146
147#define AR2315_PCI_HOST_SDRAM_BASEADDR 0x20000000
148
149
150#define AR2315_PCI_HOST_MBAR0 0x10000000
151
152#define AR2315_PCI_HOST_MBAR1 AR2315_PCI_HOST_SDRAM_BASEADDR
153
154#define AR2315_PCI_HOST_MBAR2 0x30000000
155
156struct ar2315_pci_ctrl {
157 void __iomem *cfg_mem;
158 void __iomem *mmr_mem;
159 unsigned irq;
160 unsigned irq_ext;
161 struct irq_domain *domain;
162 struct pci_controller pci_ctrl;
163 struct resource mem_res;
164 struct resource io_res;
165};
166
167static inline dma_addr_t ar2315_dev_offset(struct device *dev)
168{
169 if (dev && dev_is_pci(dev))
170 return AR2315_PCI_HOST_SDRAM_BASEADDR;
171 return 0;
172}
173
174dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
175{
176 return paddr + ar2315_dev_offset(dev);
177}
178
179phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dma_addr)
180{
181 return dma_addr - ar2315_dev_offset(dev);
182}
183
184static inline struct ar2315_pci_ctrl *ar2315_pci_bus_to_apc(struct pci_bus *bus)
185{
186 struct pci_controller *hose = bus->sysdata;
187
188 return container_of(hose, struct ar2315_pci_ctrl, pci_ctrl);
189}
190
191static inline u32 ar2315_pci_reg_read(struct ar2315_pci_ctrl *apc, u32 reg)
192{
193 return __raw_readl(apc->mmr_mem + reg);
194}
195
196static inline void ar2315_pci_reg_write(struct ar2315_pci_ctrl *apc, u32 reg,
197 u32 val)
198{
199 __raw_writel(val, apc->mmr_mem + reg);
200}
201
202static inline void ar2315_pci_reg_mask(struct ar2315_pci_ctrl *apc, u32 reg,
203 u32 mask, u32 val)
204{
205 u32 ret = ar2315_pci_reg_read(apc, reg);
206
207 ret &= ~mask;
208 ret |= val;
209 ar2315_pci_reg_write(apc, reg, ret);
210}
211
212static int ar2315_pci_cfg_access(struct ar2315_pci_ctrl *apc, unsigned devfn,
213 int where, int size, u32 *ptr, bool write)
214{
215 int func = PCI_FUNC(devfn);
216 int dev = PCI_SLOT(devfn);
217 u32 addr = (1 << (13 + dev)) | (func << 8) | (where & ~3);
218 u32 mask = 0xffffffff >> 8 * (4 - size);
219 u32 sh = (where & 3) * 8;
220 u32 value, isr;
221
222
223 if (addr >= AR2315_PCI_CFG_SIZE || dev > 18)
224 return PCIBIOS_DEVICE_NOT_FOUND;
225
226
227 ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT);
228
229 ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG, 0,
230 AR2315_PCIMISC_CFG_SEL);
231
232 mb();
233
234 value = __raw_readl(apc->cfg_mem + addr);
235
236 isr = ar2315_pci_reg_read(apc, AR2315_PCI_ISR);
237
238 if (isr & AR2315_PCI_INT_ABORT)
239 goto exit_err;
240
241 if (write) {
242 value = (value & ~(mask << sh)) | *ptr << sh;
243 __raw_writel(value, apc->cfg_mem + addr);
244 isr = ar2315_pci_reg_read(apc, AR2315_PCI_ISR);
245 if (isr & AR2315_PCI_INT_ABORT)
246 goto exit_err;
247 } else {
248 *ptr = (value >> sh) & mask;
249 }
250
251 goto exit;
252
253exit_err:
254 ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT);
255 if (!write)
256 *ptr = 0xffffffff;
257
258exit:
259
260 ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG, AR2315_PCIMISC_CFG_SEL,
261 0);
262
263 return isr & AR2315_PCI_INT_ABORT ? PCIBIOS_DEVICE_NOT_FOUND :
264 PCIBIOS_SUCCESSFUL;
265}
266
267static inline int ar2315_pci_local_cfg_rd(struct ar2315_pci_ctrl *apc,
268 unsigned devfn, int where, u32 *val)
269{
270 return ar2315_pci_cfg_access(apc, devfn, where, sizeof(u32), val,
271 false);
272}
273
274static inline int ar2315_pci_local_cfg_wr(struct ar2315_pci_ctrl *apc,
275 unsigned devfn, int where, u32 val)
276{
277 return ar2315_pci_cfg_access(apc, devfn, where, sizeof(u32), &val,
278 true);
279}
280
281static int ar2315_pci_cfg_read(struct pci_bus *bus, unsigned devfn, int where,
282 int size, u32 *value)
283{
284 struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(bus);
285
286 if (PCI_SLOT(devfn) == AR2315_PCI_HOST_SLOT)
287 return PCIBIOS_DEVICE_NOT_FOUND;
288
289 return ar2315_pci_cfg_access(apc, devfn, where, size, value, false);
290}
291
292static int ar2315_pci_cfg_write(struct pci_bus *bus, unsigned devfn, int where,
293 int size, u32 value)
294{
295 struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(bus);
296
297 if (PCI_SLOT(devfn) == AR2315_PCI_HOST_SLOT)
298 return PCIBIOS_DEVICE_NOT_FOUND;
299
300 return ar2315_pci_cfg_access(apc, devfn, where, size, &value, true);
301}
302
303static struct pci_ops ar2315_pci_ops = {
304 .read = ar2315_pci_cfg_read,
305 .write = ar2315_pci_cfg_write,
306};
307
308static int ar2315_pci_host_setup(struct ar2315_pci_ctrl *apc)
309{
310 unsigned devfn = PCI_DEVFN(AR2315_PCI_HOST_SLOT, 0);
311 int res;
312 u32 id;
313
314 res = ar2315_pci_local_cfg_rd(apc, devfn, PCI_VENDOR_ID, &id);
315 if (res != PCIBIOS_SUCCESSFUL || id != AR2315_PCI_HOST_DEVID)
316 return -ENODEV;
317
318
319 ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_0,
320 AR2315_PCI_HOST_MBAR0);
321 ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_1,
322 AR2315_PCI_HOST_MBAR1);
323 ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_2,
324 AR2315_PCI_HOST_MBAR2);
325
326
327 ar2315_pci_local_cfg_wr(apc, devfn, PCI_COMMAND, PCI_COMMAND_MEMORY |
328 PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL |
329 PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY |
330 PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK);
331
332 return 0;
333}
334
335static void ar2315_pci_irq_handler(struct irq_desc *desc)
336{
337 struct ar2315_pci_ctrl *apc = irq_desc_get_handler_data(desc);
338 u32 pending = ar2315_pci_reg_read(apc, AR2315_PCI_ISR) &
339 ar2315_pci_reg_read(apc, AR2315_PCI_IMR);
340 unsigned pci_irq = 0;
341
342 if (pending)
343 pci_irq = irq_find_mapping(apc->domain, __ffs(pending));
344
345 if (pci_irq)
346 generic_handle_irq(pci_irq);
347 else
348 spurious_interrupt();
349}
350
351static void ar2315_pci_irq_mask(struct irq_data *d)
352{
353 struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d);
354
355 ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, BIT(d->hwirq), 0);
356}
357
358static void ar2315_pci_irq_mask_ack(struct irq_data *d)
359{
360 struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d);
361 u32 m = BIT(d->hwirq);
362
363 ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, m, 0);
364 ar2315_pci_reg_write(apc, AR2315_PCI_ISR, m);
365}
366
367static void ar2315_pci_irq_unmask(struct irq_data *d)
368{
369 struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d);
370
371 ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, 0, BIT(d->hwirq));
372}
373
374static struct irq_chip ar2315_pci_irq_chip = {
375 .name = "AR2315-PCI",
376 .irq_mask = ar2315_pci_irq_mask,
377 .irq_mask_ack = ar2315_pci_irq_mask_ack,
378 .irq_unmask = ar2315_pci_irq_unmask,
379};
380
381static int ar2315_pci_irq_map(struct irq_domain *d, unsigned irq,
382 irq_hw_number_t hw)
383{
384 irq_set_chip_and_handler(irq, &ar2315_pci_irq_chip, handle_level_irq);
385 irq_set_chip_data(irq, d->host_data);
386 return 0;
387}
388
389static struct irq_domain_ops ar2315_pci_irq_domain_ops = {
390 .map = ar2315_pci_irq_map,
391};
392
393static void ar2315_pci_irq_init(struct ar2315_pci_ctrl *apc)
394{
395 ar2315_pci_reg_mask(apc, AR2315_PCI_IER, AR2315_PCI_IER_ENABLE, 0);
396 ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, (AR2315_PCI_INT_ABORT |
397 AR2315_PCI_INT_EXT), 0);
398
399 apc->irq_ext = irq_create_mapping(apc->domain, AR2315_PCI_IRQ_EXT);
400
401 irq_set_chained_handler_and_data(apc->irq, ar2315_pci_irq_handler,
402 apc);
403
404
405
406 ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT |
407 AR2315_PCI_INT_EXT);
408 ar2315_pci_reg_mask(apc, AR2315_PCI_IER, 0, AR2315_PCI_IER_ENABLE);
409}
410
411static int ar2315_pci_probe(struct platform_device *pdev)
412{
413 struct ar2315_pci_ctrl *apc;
414 struct device *dev = &pdev->dev;
415 struct resource *res;
416 int irq, err;
417
418 apc = devm_kzalloc(dev, sizeof(*apc), GFP_KERNEL);
419 if (!apc)
420 return -ENOMEM;
421
422 irq = platform_get_irq(pdev, 0);
423 if (irq < 0)
424 return -EINVAL;
425 apc->irq = irq;
426
427 apc->mmr_mem = devm_platform_ioremap_resource_byname(pdev,
428 "ar2315-pci-ctrl");
429 if (IS_ERR(apc->mmr_mem))
430 return PTR_ERR(apc->mmr_mem);
431
432 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
433 "ar2315-pci-ext");
434 if (!res)
435 return -EINVAL;
436
437 apc->mem_res.name = "AR2315 PCI mem space";
438 apc->mem_res.parent = res;
439 apc->mem_res.start = res->start;
440 apc->mem_res.end = res->end;
441 apc->mem_res.flags = IORESOURCE_MEM;
442
443
444 apc->cfg_mem = devm_ioremap(dev, res->start,
445 AR2315_PCI_CFG_SIZE);
446 if (!apc->cfg_mem) {
447 dev_err(dev, "failed to remap PCI config space\n");
448 return -ENOMEM;
449 }
450
451
452 ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG,
453 AR2315_PCIMISC_RST_MODE,
454 AR2315_PCIRST_LOW);
455 msleep(100);
456
457
458 ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG,
459 AR2315_PCIMISC_RST_MODE,
460 AR2315_PCIRST_HIGH | AR2315_PCICACHE_DIS | 0x8);
461
462 ar2315_pci_reg_write(apc, AR2315_PCI_UNCACHE_CFG,
463 0x1E |
464 (1 << 5) |
465 (0x2 << 30) );
466 ar2315_pci_reg_read(apc, AR2315_PCI_UNCACHE_CFG);
467
468 msleep(500);
469
470 err = ar2315_pci_host_setup(apc);
471 if (err)
472 return err;
473
474 apc->domain = irq_domain_add_linear(NULL, AR2315_PCI_IRQ_COUNT,
475 &ar2315_pci_irq_domain_ops, apc);
476 if (!apc->domain) {
477 dev_err(dev, "failed to add IRQ domain\n");
478 return -ENOMEM;
479 }
480
481 ar2315_pci_irq_init(apc);
482
483
484 apc->io_res.name = "AR2315 IO space";
485 apc->io_res.start = 0;
486 apc->io_res.end = 0;
487 apc->io_res.flags = IORESOURCE_IO;
488
489 apc->pci_ctrl.pci_ops = &ar2315_pci_ops;
490 apc->pci_ctrl.mem_resource = &apc->mem_res;
491 apc->pci_ctrl.io_resource = &apc->io_res;
492
493 register_pci_controller(&apc->pci_ctrl);
494
495 dev_info(dev, "register PCI controller\n");
496
497 return 0;
498}
499
500static struct platform_driver ar2315_pci_driver = {
501 .probe = ar2315_pci_probe,
502 .driver = {
503 .name = "ar2315-pci",
504 },
505};
506
507static int __init ar2315_pci_init(void)
508{
509 return platform_driver_register(&ar2315_pci_driver);
510}
511arch_initcall(ar2315_pci_init);
512
513int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
514{
515 struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(dev->bus);
516
517 return slot ? 0 : apc->irq_ext;
518}
519
520int pcibios_plat_dev_init(struct pci_dev *dev)
521{
522 return 0;
523}
524