pci-ixp4xx.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for Intel IXP4xx PCI host controller
  4. *
  5. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  6. *
  7. * Based on the IXP4xx arch/arm/mach-ixp4xx/common-pci.c driver
  8. * Copyright (C) 2002 Intel Corporation
  9. * Copyright (C) 2003 Greg Ungerer <gerg@linux-m68k.org>
  10. * Copyright (C) 2003-2004 MontaVista Software, Inc.
  11. * Copyright (C) 2005 Deepak Saxena <dsaxena@plexity.net>
  12. * Copyright (C) 2005 Alessandro Zummo <a.zummo@towertech.it>
  13. *
  14. * TODO:
  15. * - Test IO-space access
  16. * - DMA support
  17. */
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/of.h>
  22. #include <linux/of_pci.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/bits.h>
  27. #include "../pci.h"
  28. /* Register offsets */
  29. #define IXP4XX_PCI_NP_AD 0x00
  30. #define IXP4XX_PCI_NP_CBE 0x04
  31. #define IXP4XX_PCI_NP_WDATA 0x08
  32. #define IXP4XX_PCI_NP_RDATA 0x0c
  33. #define IXP4XX_PCI_CRP_AD_CBE 0x10
  34. #define IXP4XX_PCI_CRP_WDATA 0x14
  35. #define IXP4XX_PCI_CRP_RDATA 0x18
  36. #define IXP4XX_PCI_CSR 0x1c
  37. #define IXP4XX_PCI_ISR 0x20
  38. #define IXP4XX_PCI_INTEN 0x24
  39. #define IXP4XX_PCI_DMACTRL 0x28
  40. #define IXP4XX_PCI_AHBMEMBASE 0x2c
  41. #define IXP4XX_PCI_AHBIOBASE 0x30
  42. #define IXP4XX_PCI_PCIMEMBASE 0x34
  43. #define IXP4XX_PCI_AHBDOORBELL 0x38
  44. #define IXP4XX_PCI_PCIDOORBELL 0x3c
  45. #define IXP4XX_PCI_ATPDMA0_AHBADDR 0x40
  46. #define IXP4XX_PCI_ATPDMA0_PCIADDR 0x44
  47. #define IXP4XX_PCI_ATPDMA0_LENADDR 0x48
  48. #define IXP4XX_PCI_ATPDMA1_AHBADDR 0x4c
  49. #define IXP4XX_PCI_ATPDMA1_PCIADDR 0x50
  50. #define IXP4XX_PCI_ATPDMA1_LENADDR 0x54
  51. /* CSR bit definitions */
  52. #define IXP4XX_PCI_CSR_HOST BIT(0)
  53. #define IXP4XX_PCI_CSR_ARBEN BIT(1)
  54. #define IXP4XX_PCI_CSR_ADS BIT(2)
  55. #define IXP4XX_PCI_CSR_PDS BIT(3)
  56. #define IXP4XX_PCI_CSR_ABE BIT(4)
  57. #define IXP4XX_PCI_CSR_DBT BIT(5)
  58. #define IXP4XX_PCI_CSR_ASE BIT(8)
  59. #define IXP4XX_PCI_CSR_IC BIT(15)
  60. #define IXP4XX_PCI_CSR_PRST BIT(16)
  61. /* ISR (Interrupt status) Register bit definitions */
  62. #define IXP4XX_PCI_ISR_PSE BIT(0)
  63. #define IXP4XX_PCI_ISR_PFE BIT(1)
  64. #define IXP4XX_PCI_ISR_PPE BIT(2)
  65. #define IXP4XX_PCI_ISR_AHBE BIT(3)
  66. #define IXP4XX_PCI_ISR_APDC BIT(4)
  67. #define IXP4XX_PCI_ISR_PADC BIT(5)
  68. #define IXP4XX_PCI_ISR_ADB BIT(6)
  69. #define IXP4XX_PCI_ISR_PDB BIT(7)
  70. /* INTEN (Interrupt Enable) Register bit definitions */
  71. #define IXP4XX_PCI_INTEN_PSE BIT(0)
  72. #define IXP4XX_PCI_INTEN_PFE BIT(1)
  73. #define IXP4XX_PCI_INTEN_PPE BIT(2)
  74. #define IXP4XX_PCI_INTEN_AHBE BIT(3)
  75. #define IXP4XX_PCI_INTEN_APDC BIT(4)
  76. #define IXP4XX_PCI_INTEN_PADC BIT(5)
  77. #define IXP4XX_PCI_INTEN_ADB BIT(6)
  78. #define IXP4XX_PCI_INTEN_PDB BIT(7)
  79. /* Shift value for byte enable on NP cmd/byte enable register */
  80. #define IXP4XX_PCI_NP_CBE_BESL 4
  81. /* PCI commands supported by NP access unit */
  82. #define NP_CMD_IOREAD 0x2
  83. #define NP_CMD_IOWRITE 0x3
  84. #define NP_CMD_CONFIGREAD 0xa
  85. #define NP_CMD_CONFIGWRITE 0xb
  86. #define NP_CMD_MEMREAD 0x6
  87. #define NP_CMD_MEMWRITE 0x7
  88. /* Constants for CRP access into local config space */
  89. #define CRP_AD_CBE_BESL 20
  90. #define CRP_AD_CBE_WRITE 0x00010000
  91. /* Special PCI configuration space registers for this controller */
  92. #define IXP4XX_PCI_RTOTTO 0x40
  93. struct ixp4xx_pci {
  94. struct device *dev;
  95. void __iomem *base;
  96. bool errata_hammer;
  97. bool host_mode;
  98. };
  99. /*
  100. * The IXP4xx has a peculiar address bus that will change the
  101. * byte order on SoC peripherals depending on whether the device
  102. * operates in big-endian or little-endian mode. That means that
  103. * readl() and writel() that always use little-endian access
  104. * will not work for SoC peripherals such as the PCI controller
  105. * when used in big-endian mode. The accesses to the individual
  106. * PCI devices on the other hand, are always little-endian and
  107. * can use readl() and writel().
  108. *
  109. * For local AHB bus access we need to use __raw_[readl|writel]()
  110. * to make sure that we access the SoC devices in the CPU native
  111. * endianness.
  112. */
  113. static inline u32 ixp4xx_readl(struct ixp4xx_pci *p, u32 reg)
  114. {
  115. return __raw_readl(p->base + reg);
  116. }
  117. static inline void ixp4xx_writel(struct ixp4xx_pci *p, u32 reg, u32 val)
  118. {
  119. __raw_writel(val, p->base + reg);
  120. }
  121. static int ixp4xx_pci_check_master_abort(struct ixp4xx_pci *p)
  122. {
  123. u32 isr = ixp4xx_readl(p, IXP4XX_PCI_ISR);
  124. if (isr & IXP4XX_PCI_ISR_PFE) {
  125. /* Make sure the master abort bit is reset */
  126. ixp4xx_writel(p, IXP4XX_PCI_ISR, IXP4XX_PCI_ISR_PFE);
  127. dev_dbg(p->dev, "master abort detected\n");
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. static int ixp4xx_pci_read_indirect(struct ixp4xx_pci *p, u32 addr, u32 cmd, u32 *data)
  133. {
  134. ixp4xx_writel(p, IXP4XX_PCI_NP_AD, addr);
  135. if (p->errata_hammer) {
  136. int i;
  137. /*
  138. * PCI workaround - only works if NP PCI space reads have
  139. * no side effects. Hammer the register and read twice 8
  140. * times. last one will be good.
  141. */
  142. for (i = 0; i < 8; i++) {
  143. ixp4xx_writel(p, IXP4XX_PCI_NP_CBE, cmd);
  144. *data = ixp4xx_readl(p, IXP4XX_PCI_NP_RDATA);
  145. *data = ixp4xx_readl(p, IXP4XX_PCI_NP_RDATA);
  146. }
  147. } else {
  148. ixp4xx_writel(p, IXP4XX_PCI_NP_CBE, cmd);
  149. *data = ixp4xx_readl(p, IXP4XX_PCI_NP_RDATA);
  150. }
  151. return ixp4xx_pci_check_master_abort(p);
  152. }
  153. static int ixp4xx_pci_write_indirect(struct ixp4xx_pci *p, u32 addr, u32 cmd, u32 data)
  154. {
  155. ixp4xx_writel(p, IXP4XX_PCI_NP_AD, addr);
  156. /* Set up the write */
  157. ixp4xx_writel(p, IXP4XX_PCI_NP_CBE, cmd);
  158. /* Execute the write by writing to NP_WDATA */
  159. ixp4xx_writel(p, IXP4XX_PCI_NP_WDATA, data);
  160. return ixp4xx_pci_check_master_abort(p);
  161. }
  162. static u32 ixp4xx_config_addr(u8 bus_num, u16 devfn, int where)
  163. {
  164. /* Root bus is always 0 in this hardware */
  165. if (bus_num == 0) {
  166. /* type 0 */
  167. return (PCI_CONF1_ADDRESS(0, 0, PCI_FUNC(devfn), where) &
  168. ~PCI_CONF1_ENABLE) | BIT(32-PCI_SLOT(devfn));
  169. } else {
  170. /* type 1 */
  171. return (PCI_CONF1_ADDRESS(bus_num, PCI_SLOT(devfn),
  172. PCI_FUNC(devfn), where) &
  173. ~PCI_CONF1_ENABLE) | 1;
  174. }
  175. }
  176. /*
  177. * CRP functions are "Controller Configuration Port" accesses
  178. * initiated from within this driver itself to read/write PCI
  179. * control information in the config space.
  180. */
  181. static u32 ixp4xx_crp_byte_lane_enable_bits(u32 n, int size)
  182. {
  183. if (size == 1)
  184. return (0xf & ~BIT(n)) << CRP_AD_CBE_BESL;
  185. if (size == 2)
  186. return (0xf & ~(BIT(n) | BIT(n+1))) << CRP_AD_CBE_BESL;
  187. if (size == 4)
  188. return 0;
  189. return 0xffffffff;
  190. }
  191. static int ixp4xx_crp_read_config(struct ixp4xx_pci *p, int where, int size,
  192. u32 *value)
  193. {
  194. u32 n, cmd, val;
  195. n = where % 4;
  196. cmd = where & ~3;
  197. dev_dbg(p->dev, "%s from %d size %d cmd %08x\n",
  198. __func__, where, size, cmd);
  199. ixp4xx_writel(p, IXP4XX_PCI_CRP_AD_CBE, cmd);
  200. val = ixp4xx_readl(p, IXP4XX_PCI_CRP_RDATA);
  201. val >>= (8*n);
  202. switch (size) {
  203. case 1:
  204. val &= U8_MAX;
  205. dev_dbg(p->dev, "%s read byte %02x\n", __func__, val);
  206. break;
  207. case 2:
  208. val &= U16_MAX;
  209. dev_dbg(p->dev, "%s read word %04x\n", __func__, val);
  210. break;
  211. case 4:
  212. val &= U32_MAX;
  213. dev_dbg(p->dev, "%s read long %08x\n", __func__, val);
  214. break;
  215. default:
  216. /* Should not happen */
  217. dev_err(p->dev, "%s illegal size\n", __func__);
  218. return PCIBIOS_DEVICE_NOT_FOUND;
  219. }
  220. *value = val;
  221. return PCIBIOS_SUCCESSFUL;
  222. }
  223. static int ixp4xx_crp_write_config(struct ixp4xx_pci *p, int where, int size,
  224. u32 value)
  225. {
  226. u32 n, cmd, val;
  227. n = where % 4;
  228. cmd = ixp4xx_crp_byte_lane_enable_bits(n, size);
  229. if (cmd == 0xffffffff)
  230. return PCIBIOS_BAD_REGISTER_NUMBER;
  231. cmd |= where & ~3;
  232. cmd |= CRP_AD_CBE_WRITE;
  233. val = value << (8*n);
  234. dev_dbg(p->dev, "%s to %d size %d cmd %08x val %08x\n",
  235. __func__, where, size, cmd, val);
  236. ixp4xx_writel(p, IXP4XX_PCI_CRP_AD_CBE, cmd);
  237. ixp4xx_writel(p, IXP4XX_PCI_CRP_WDATA, val);
  238. return PCIBIOS_SUCCESSFUL;
  239. }
  240. /*
  241. * Then follows the functions that read and write from the common PCI
  242. * configuration space.
  243. */
  244. static u32 ixp4xx_byte_lane_enable_bits(u32 n, int size)
  245. {
  246. if (size == 1)
  247. return (0xf & ~BIT(n)) << 4;
  248. if (size == 2)
  249. return (0xf & ~(BIT(n) | BIT(n+1))) << 4;
  250. if (size == 4)
  251. return 0;
  252. return 0xffffffff;
  253. }
  254. static int ixp4xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  255. int where, int size, u32 *value)
  256. {
  257. struct ixp4xx_pci *p = bus->sysdata;
  258. u32 n, addr, val, cmd;
  259. u8 bus_num = bus->number;
  260. int ret;
  261. *value = 0xffffffff;
  262. n = where % 4;
  263. cmd = ixp4xx_byte_lane_enable_bits(n, size);
  264. if (cmd == 0xffffffff)
  265. return PCIBIOS_BAD_REGISTER_NUMBER;
  266. addr = ixp4xx_config_addr(bus_num, devfn, where);
  267. cmd |= NP_CMD_CONFIGREAD;
  268. dev_dbg(p->dev, "read_config from %d size %d dev %d:%d:%d address: %08x cmd: %08x\n",
  269. where, size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn), addr, cmd);
  270. ret = ixp4xx_pci_read_indirect(p, addr, cmd, &val);
  271. if (ret)
  272. return PCIBIOS_DEVICE_NOT_FOUND;
  273. val >>= (8*n);
  274. switch (size) {
  275. case 1:
  276. val &= U8_MAX;
  277. dev_dbg(p->dev, "%s read byte %02x\n", __func__, val);
  278. break;
  279. case 2:
  280. val &= U16_MAX;
  281. dev_dbg(p->dev, "%s read word %04x\n", __func__, val);
  282. break;
  283. case 4:
  284. val &= U32_MAX;
  285. dev_dbg(p->dev, "%s read long %08x\n", __func__, val);
  286. break;
  287. default:
  288. /* Should not happen */
  289. dev_err(p->dev, "%s illegal size\n", __func__);
  290. return PCIBIOS_DEVICE_NOT_FOUND;
  291. }
  292. *value = val;
  293. return PCIBIOS_SUCCESSFUL;
  294. }
  295. static int ixp4xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  296. int where, int size, u32 value)
  297. {
  298. struct ixp4xx_pci *p = bus->sysdata;
  299. u32 n, addr, val, cmd;
  300. u8 bus_num = bus->number;
  301. int ret;
  302. n = where % 4;
  303. cmd = ixp4xx_byte_lane_enable_bits(n, size);
  304. if (cmd == 0xffffffff)
  305. return PCIBIOS_BAD_REGISTER_NUMBER;
  306. addr = ixp4xx_config_addr(bus_num, devfn, where);
  307. cmd |= NP_CMD_CONFIGWRITE;
  308. val = value << (8*n);
  309. dev_dbg(p->dev, "write_config_byte %#x to %d size %d dev %d:%d:%d addr: %08x cmd %08x\n",
  310. value, where, size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn), addr, cmd);
  311. ret = ixp4xx_pci_write_indirect(p, addr, cmd, val);
  312. if (ret)
  313. return PCIBIOS_DEVICE_NOT_FOUND;
  314. return PCIBIOS_SUCCESSFUL;
  315. }
  316. static struct pci_ops ixp4xx_pci_ops = {
  317. .read = ixp4xx_pci_read_config,
  318. .write = ixp4xx_pci_write_config,
  319. };
  320. static u32 ixp4xx_pci_addr_to_64mconf(phys_addr_t addr)
  321. {
  322. u8 base;
  323. base = ((addr & 0xff000000) >> 24);
  324. return (base << 24) | ((base + 1) << 16)
  325. | ((base + 2) << 8) | (base + 3);
  326. }
  327. static int ixp4xx_pci_parse_map_ranges(struct ixp4xx_pci *p)
  328. {
  329. struct device *dev = p->dev;
  330. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(p);
  331. struct resource_entry *win;
  332. struct resource *res;
  333. phys_addr_t addr;
  334. win = resource_list_first_type(&bridge->windows, IORESOURCE_MEM);
  335. if (win) {
  336. u32 pcimembase;
  337. res = win->res;
  338. addr = res->start - win->offset;
  339. if (res->flags & IORESOURCE_PREFETCH)
  340. res->name = "IXP4xx PCI PRE-MEM";
  341. else
  342. res->name = "IXP4xx PCI NON-PRE-MEM";
  343. dev_dbg(dev, "%s window %pR, bus addr %pa\n",
  344. res->name, res, &addr);
  345. if (resource_size(res) != SZ_64M) {
  346. dev_err(dev, "memory range is not 64MB\n");
  347. return -EINVAL;
  348. }
  349. pcimembase = ixp4xx_pci_addr_to_64mconf(addr);
  350. /* Commit configuration */
  351. ixp4xx_writel(p, IXP4XX_PCI_PCIMEMBASE, pcimembase);
  352. } else {
  353. dev_err(dev, "no AHB memory mapping defined\n");
  354. }
  355. win = resource_list_first_type(&bridge->windows, IORESOURCE_IO);
  356. if (win) {
  357. res = win->res;
  358. addr = pci_pio_to_address(res->start);
  359. if (addr & 0xff) {
  360. dev_err(dev, "IO mem at uneven address: %pa\n", &addr);
  361. return -EINVAL;
  362. }
  363. res->name = "IXP4xx PCI IO MEM";
  364. /*
  365. * Setup I/O space location for PCI->AHB access, the
  366. * upper 24 bits of the address goes into the lower
  367. * 24 bits of this register.
  368. */
  369. ixp4xx_writel(p, IXP4XX_PCI_AHBIOBASE, (addr >> 8));
  370. } else {
  371. dev_info(dev, "no IO space AHB memory mapping defined\n");
  372. }
  373. return 0;
  374. }
  375. static int ixp4xx_pci_parse_map_dma_ranges(struct ixp4xx_pci *p)
  376. {
  377. struct device *dev = p->dev;
  378. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(p);
  379. struct resource_entry *win;
  380. struct resource *res;
  381. phys_addr_t addr;
  382. u32 ahbmembase;
  383. win = resource_list_first_type(&bridge->dma_ranges, IORESOURCE_MEM);
  384. if (win) {
  385. res = win->res;
  386. addr = res->start - win->offset;
  387. if (resource_size(res) != SZ_64M) {
  388. dev_err(dev, "DMA memory range is not 64MB\n");
  389. return -EINVAL;
  390. }
  391. dev_dbg(dev, "DMA MEM BASE: %pa\n", &addr);
  392. /*
  393. * 4 PCI-to-AHB windows of 16 MB each, write the 8 high bits
  394. * into each byte of the PCI_AHBMEMBASE register.
  395. */
  396. ahbmembase = ixp4xx_pci_addr_to_64mconf(addr);
  397. /* Commit AHB membase */
  398. ixp4xx_writel(p, IXP4XX_PCI_AHBMEMBASE, ahbmembase);
  399. } else {
  400. dev_err(dev, "no DMA memory range defined\n");
  401. }
  402. return 0;
  403. }
  404. /* Only used to get context for abort handling */
  405. static struct ixp4xx_pci *ixp4xx_pci_abort_singleton;
  406. static int ixp4xx_pci_abort_handler(unsigned long addr, unsigned int fsr,
  407. struct pt_regs *regs)
  408. {
  409. struct ixp4xx_pci *p = ixp4xx_pci_abort_singleton;
  410. u32 isr, status;
  411. int ret;
  412. isr = ixp4xx_readl(p, IXP4XX_PCI_ISR);
  413. ret = ixp4xx_crp_read_config(p, PCI_STATUS, 2, &status);
  414. if (ret) {
  415. dev_err(p->dev, "unable to read abort status\n");
  416. return -EINVAL;
  417. }
  418. dev_err(p->dev,
  419. "PCI: abort_handler addr = %#lx, isr = %#x, status = %#x\n",
  420. addr, isr, status);
  421. /* Make sure the Master Abort bit is reset */
  422. ixp4xx_writel(p, IXP4XX_PCI_ISR, IXP4XX_PCI_ISR_PFE);
  423. status |= PCI_STATUS_REC_MASTER_ABORT;
  424. ret = ixp4xx_crp_write_config(p, PCI_STATUS, 2, status);
  425. if (ret)
  426. dev_err(p->dev, "unable to clear abort status bit\n");
  427. /*
  428. * If it was an imprecise abort, then we need to correct the
  429. * return address to be _after_ the instruction.
  430. */
  431. if (fsr & (1 << 10)) {
  432. dev_err(p->dev, "imprecise abort\n");
  433. regs->ARM_pc += 4;
  434. }
  435. return 0;
  436. }
  437. static int __init ixp4xx_pci_probe(struct platform_device *pdev)
  438. {
  439. struct device *dev = &pdev->dev;
  440. struct device_node *np = dev->of_node;
  441. struct ixp4xx_pci *p;
  442. struct pci_host_bridge *host;
  443. int ret;
  444. u32 val;
  445. phys_addr_t addr;
  446. u32 basereg[4] = {
  447. PCI_BASE_ADDRESS_0,
  448. PCI_BASE_ADDRESS_1,
  449. PCI_BASE_ADDRESS_2,
  450. PCI_BASE_ADDRESS_3,
  451. };
  452. int i;
  453. host = devm_pci_alloc_host_bridge(dev, sizeof(*p));
  454. if (!host)
  455. return -ENOMEM;
  456. host->ops = &ixp4xx_pci_ops;
  457. p = pci_host_bridge_priv(host);
  458. host->sysdata = p;
  459. p->dev = dev;
  460. dev_set_drvdata(dev, p);
  461. /*
  462. * Set up quirk for erratic behaviour in the 42x variant
  463. * when accessing config space.
  464. */
  465. if (of_device_is_compatible(np, "intel,ixp42x-pci")) {
  466. p->errata_hammer = true;
  467. dev_info(dev, "activate hammering errata\n");
  468. }
  469. p->base = devm_platform_ioremap_resource(pdev, 0);
  470. if (IS_ERR(p->base))
  471. return PTR_ERR(p->base);
  472. val = ixp4xx_readl(p, IXP4XX_PCI_CSR);
  473. p->host_mode = !!(val & IXP4XX_PCI_CSR_HOST);
  474. dev_info(dev, "controller is in %s mode\n",
  475. p->host_mode ? "host" : "option");
  476. /* Hook in our fault handler for PCI errors */
  477. ixp4xx_pci_abort_singleton = p;
  478. hook_fault_code(16+6, ixp4xx_pci_abort_handler, SIGBUS, 0,
  479. "imprecise external abort");
  480. ret = ixp4xx_pci_parse_map_ranges(p);
  481. if (ret)
  482. return ret;
  483. ret = ixp4xx_pci_parse_map_dma_ranges(p);
  484. if (ret)
  485. return ret;
  486. /* This is only configured in host mode */
  487. if (p->host_mode) {
  488. addr = __pa(PAGE_OFFSET);
  489. /* This is a noop (0x00) but explains what is going on */
  490. addr |= PCI_BASE_ADDRESS_SPACE_MEMORY;
  491. for (i = 0; i < 4; i++) {
  492. /* Write this directly into the config space */
  493. ret = ixp4xx_crp_write_config(p, basereg[i], 4, addr);
  494. if (ret)
  495. dev_err(dev, "failed to set up PCI_BASE_ADDRESS_%d\n", i);
  496. else
  497. dev_info(dev, "set PCI_BASE_ADDR_%d to %pa\n", i, &addr);
  498. addr += SZ_16M;
  499. }
  500. /*
  501. * Enable CSR window at 64 MiB to allow PCI masters to continue
  502. * prefetching past the 64 MiB boundary, if all AHB to PCI
  503. * windows are consecutive.
  504. */
  505. ret = ixp4xx_crp_write_config(p, PCI_BASE_ADDRESS_4, 4, addr);
  506. if (ret)
  507. dev_err(dev, "failed to set up PCI_BASE_ADDRESS_4\n");
  508. else
  509. dev_info(dev, "set PCI_BASE_ADDR_4 to %pa\n", &addr);
  510. /*
  511. * Put the IO memory window at the very end of physical memory
  512. * at 0xfffffc00. This is when the system is trying to access IO
  513. * memory over AHB.
  514. */
  515. addr = 0xfffffc00;
  516. addr |= PCI_BASE_ADDRESS_SPACE_IO;
  517. ret = ixp4xx_crp_write_config(p, PCI_BASE_ADDRESS_5, 4, addr);
  518. if (ret)
  519. dev_err(dev, "failed to set up PCI_BASE_ADDRESS_5\n");
  520. else
  521. dev_info(dev, "set PCI_BASE_ADDR_5 to %pa\n", &addr);
  522. /*
  523. * Retry timeout to 0x80
  524. * Transfer ready timeout to 0xff
  525. */
  526. ret = ixp4xx_crp_write_config(p, IXP4XX_PCI_RTOTTO, 4,
  527. 0x000080ff);
  528. if (ret)
  529. dev_err(dev, "failed to set up TRDY limit\n");
  530. else
  531. dev_info(dev, "set TRDY limit to 0x80ff\n");
  532. }
  533. /* Clear interrupts */
  534. val = IXP4XX_PCI_ISR_PSE | IXP4XX_PCI_ISR_PFE | IXP4XX_PCI_ISR_PPE | IXP4XX_PCI_ISR_AHBE;
  535. ixp4xx_writel(p, IXP4XX_PCI_ISR, val);
  536. /*
  537. * Set Initialize Complete in PCI Control Register: allow IXP4XX to
  538. * generate PCI configuration cycles. Specify that the AHB bus is
  539. * operating in big-endian mode. Set up byte lane swapping between
  540. * little-endian PCI and the big-endian AHB bus.
  541. */
  542. val = IXP4XX_PCI_CSR_IC | IXP4XX_PCI_CSR_ABE;
  543. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  544. val |= (IXP4XX_PCI_CSR_PDS | IXP4XX_PCI_CSR_ADS);
  545. ixp4xx_writel(p, IXP4XX_PCI_CSR, val);
  546. ret = ixp4xx_crp_write_config(p, PCI_COMMAND, 2, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
  547. if (ret)
  548. dev_err(dev, "unable to initialize master and command memory\n");
  549. else
  550. dev_info(dev, "initialized as master\n");
  551. pci_host_probe(host);
  552. return 0;
  553. }
  554. static const struct of_device_id ixp4xx_pci_of_match[] = {
  555. {
  556. .compatible = "intel,ixp42x-pci",
  557. },
  558. {
  559. .compatible = "intel,ixp43x-pci",
  560. },
  561. {},
  562. };
  563. /*
  564. * This driver needs to be a builtin module with suppressed bind
  565. * attributes since the probe() is initializing a hard exception
  566. * handler and this can only be done from __init-tagged code
  567. * sections. This module cannot be removed and inserted at all.
  568. */
  569. static struct platform_driver ixp4xx_pci_driver = {
  570. .driver = {
  571. .name = "ixp4xx-pci",
  572. .suppress_bind_attrs = true,
  573. .of_match_table = ixp4xx_pci_of_match,
  574. },
  575. };
  576. builtin_platform_driver_probe(ixp4xx_pci_driver, ixp4xx_pci_probe);