pci-ixp4xx.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. #ifdef CONFIG_ARM
  192. static int ixp4xx_crp_read_config(struct ixp4xx_pci *p, int where, int size,
  193. u32 *value)
  194. {
  195. u32 n, cmd, val;
  196. n = where % 4;
  197. cmd = where & ~3;
  198. dev_dbg(p->dev, "%s from %d size %d cmd %08x\n",
  199. __func__, where, size, cmd);
  200. ixp4xx_writel(p, IXP4XX_PCI_CRP_AD_CBE, cmd);
  201. val = ixp4xx_readl(p, IXP4XX_PCI_CRP_RDATA);
  202. val >>= (8*n);
  203. switch (size) {
  204. case 1:
  205. val &= U8_MAX;
  206. dev_dbg(p->dev, "%s read byte %02x\n", __func__, val);
  207. break;
  208. case 2:
  209. val &= U16_MAX;
  210. dev_dbg(p->dev, "%s read word %04x\n", __func__, val);
  211. break;
  212. case 4:
  213. val &= U32_MAX;
  214. dev_dbg(p->dev, "%s read long %08x\n", __func__, val);
  215. break;
  216. default:
  217. /* Should not happen */
  218. dev_err(p->dev, "%s illegal size\n", __func__);
  219. return PCIBIOS_DEVICE_NOT_FOUND;
  220. }
  221. *value = val;
  222. return PCIBIOS_SUCCESSFUL;
  223. }
  224. #endif
  225. static int ixp4xx_crp_write_config(struct ixp4xx_pci *p, int where, int size,
  226. u32 value)
  227. {
  228. u32 n, cmd, val;
  229. n = where % 4;
  230. cmd = ixp4xx_crp_byte_lane_enable_bits(n, size);
  231. if (cmd == 0xffffffff)
  232. return PCIBIOS_BAD_REGISTER_NUMBER;
  233. cmd |= where & ~3;
  234. cmd |= CRP_AD_CBE_WRITE;
  235. val = value << (8*n);
  236. dev_dbg(p->dev, "%s to %d size %d cmd %08x val %08x\n",
  237. __func__, where, size, cmd, val);
  238. ixp4xx_writel(p, IXP4XX_PCI_CRP_AD_CBE, cmd);
  239. ixp4xx_writel(p, IXP4XX_PCI_CRP_WDATA, val);
  240. return PCIBIOS_SUCCESSFUL;
  241. }
  242. /*
  243. * Then follows the functions that read and write from the common PCI
  244. * configuration space.
  245. */
  246. static u32 ixp4xx_byte_lane_enable_bits(u32 n, int size)
  247. {
  248. if (size == 1)
  249. return (0xf & ~BIT(n)) << 4;
  250. if (size == 2)
  251. return (0xf & ~(BIT(n) | BIT(n+1))) << 4;
  252. if (size == 4)
  253. return 0;
  254. return 0xffffffff;
  255. }
  256. static int ixp4xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  257. int where, int size, u32 *value)
  258. {
  259. struct ixp4xx_pci *p = bus->sysdata;
  260. u32 n, addr, val, cmd;
  261. u8 bus_num = bus->number;
  262. int ret;
  263. *value = 0xffffffff;
  264. n = where % 4;
  265. cmd = ixp4xx_byte_lane_enable_bits(n, size);
  266. if (cmd == 0xffffffff)
  267. return PCIBIOS_BAD_REGISTER_NUMBER;
  268. addr = ixp4xx_config_addr(bus_num, devfn, where);
  269. cmd |= NP_CMD_CONFIGREAD;
  270. dev_dbg(p->dev, "read_config from %d size %d dev %d:%d:%d address: %08x cmd: %08x\n",
  271. where, size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn), addr, cmd);
  272. ret = ixp4xx_pci_read_indirect(p, addr, cmd, &val);
  273. if (ret)
  274. return PCIBIOS_DEVICE_NOT_FOUND;
  275. val >>= (8*n);
  276. switch (size) {
  277. case 1:
  278. val &= U8_MAX;
  279. dev_dbg(p->dev, "%s read byte %02x\n", __func__, val);
  280. break;
  281. case 2:
  282. val &= U16_MAX;
  283. dev_dbg(p->dev, "%s read word %04x\n", __func__, val);
  284. break;
  285. case 4:
  286. val &= U32_MAX;
  287. dev_dbg(p->dev, "%s read long %08x\n", __func__, val);
  288. break;
  289. default:
  290. /* Should not happen */
  291. dev_err(p->dev, "%s illegal size\n", __func__);
  292. return PCIBIOS_DEVICE_NOT_FOUND;
  293. }
  294. *value = val;
  295. return PCIBIOS_SUCCESSFUL;
  296. }
  297. static int ixp4xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  298. int where, int size, u32 value)
  299. {
  300. struct ixp4xx_pci *p = bus->sysdata;
  301. u32 n, addr, val, cmd;
  302. u8 bus_num = bus->number;
  303. int ret;
  304. n = where % 4;
  305. cmd = ixp4xx_byte_lane_enable_bits(n, size);
  306. if (cmd == 0xffffffff)
  307. return PCIBIOS_BAD_REGISTER_NUMBER;
  308. addr = ixp4xx_config_addr(bus_num, devfn, where);
  309. cmd |= NP_CMD_CONFIGWRITE;
  310. val = value << (8*n);
  311. dev_dbg(p->dev, "write_config_byte %#x to %d size %d dev %d:%d:%d addr: %08x cmd %08x\n",
  312. value, where, size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn), addr, cmd);
  313. ret = ixp4xx_pci_write_indirect(p, addr, cmd, val);
  314. if (ret)
  315. return PCIBIOS_DEVICE_NOT_FOUND;
  316. return PCIBIOS_SUCCESSFUL;
  317. }
  318. static struct pci_ops ixp4xx_pci_ops = {
  319. .read = ixp4xx_pci_read_config,
  320. .write = ixp4xx_pci_write_config,
  321. };
  322. static u32 ixp4xx_pci_addr_to_64mconf(phys_addr_t addr)
  323. {
  324. u8 base;
  325. base = ((addr & 0xff000000) >> 24);
  326. return (base << 24) | ((base + 1) << 16)
  327. | ((base + 2) << 8) | (base + 3);
  328. }
  329. static int ixp4xx_pci_parse_map_ranges(struct ixp4xx_pci *p)
  330. {
  331. struct device *dev = p->dev;
  332. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(p);
  333. struct resource_entry *win;
  334. struct resource *res;
  335. phys_addr_t addr;
  336. win = resource_list_first_type(&bridge->windows, IORESOURCE_MEM);
  337. if (win) {
  338. u32 pcimembase;
  339. res = win->res;
  340. addr = res->start - win->offset;
  341. if (res->flags & IORESOURCE_PREFETCH)
  342. res->name = "IXP4xx PCI PRE-MEM";
  343. else
  344. res->name = "IXP4xx PCI NON-PRE-MEM";
  345. dev_dbg(dev, "%s window %pR, bus addr %pa\n",
  346. res->name, res, &addr);
  347. if (resource_size(res) != SZ_64M) {
  348. dev_err(dev, "memory range is not 64MB\n");
  349. return -EINVAL;
  350. }
  351. pcimembase = ixp4xx_pci_addr_to_64mconf(addr);
  352. /* Commit configuration */
  353. ixp4xx_writel(p, IXP4XX_PCI_PCIMEMBASE, pcimembase);
  354. } else {
  355. dev_err(dev, "no AHB memory mapping defined\n");
  356. }
  357. win = resource_list_first_type(&bridge->windows, IORESOURCE_IO);
  358. if (win) {
  359. res = win->res;
  360. addr = pci_pio_to_address(res->start);
  361. if (addr & 0xff) {
  362. dev_err(dev, "IO mem at uneven address: %pa\n", &addr);
  363. return -EINVAL;
  364. }
  365. res->name = "IXP4xx PCI IO MEM";
  366. /*
  367. * Setup I/O space location for PCI->AHB access, the
  368. * upper 24 bits of the address goes into the lower
  369. * 24 bits of this register.
  370. */
  371. ixp4xx_writel(p, IXP4XX_PCI_AHBIOBASE, (addr >> 8));
  372. } else {
  373. dev_info(dev, "no IO space AHB memory mapping defined\n");
  374. }
  375. return 0;
  376. }
  377. static int ixp4xx_pci_parse_map_dma_ranges(struct ixp4xx_pci *p)
  378. {
  379. struct device *dev = p->dev;
  380. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(p);
  381. struct resource_entry *win;
  382. struct resource *res;
  383. phys_addr_t addr;
  384. u32 ahbmembase;
  385. win = resource_list_first_type(&bridge->dma_ranges, IORESOURCE_MEM);
  386. if (win) {
  387. res = win->res;
  388. addr = res->start - win->offset;
  389. if (resource_size(res) != SZ_64M) {
  390. dev_err(dev, "DMA memory range is not 64MB\n");
  391. return -EINVAL;
  392. }
  393. dev_dbg(dev, "DMA MEM BASE: %pa\n", &addr);
  394. /*
  395. * 4 PCI-to-AHB windows of 16 MB each, write the 8 high bits
  396. * into each byte of the PCI_AHBMEMBASE register.
  397. */
  398. ahbmembase = ixp4xx_pci_addr_to_64mconf(addr);
  399. /* Commit AHB membase */
  400. ixp4xx_writel(p, IXP4XX_PCI_AHBMEMBASE, ahbmembase);
  401. } else {
  402. dev_err(dev, "no DMA memory range defined\n");
  403. }
  404. return 0;
  405. }
  406. #ifdef CONFIG_ARM
  407. /* Only used to get context for abort handling */
  408. static struct ixp4xx_pci *ixp4xx_pci_abort_singleton;
  409. static int ixp4xx_pci_abort_handler(unsigned long addr, unsigned int fsr,
  410. struct pt_regs *regs)
  411. {
  412. struct ixp4xx_pci *p = ixp4xx_pci_abort_singleton;
  413. u32 isr, status;
  414. int ret;
  415. isr = ixp4xx_readl(p, IXP4XX_PCI_ISR);
  416. ret = ixp4xx_crp_read_config(p, PCI_STATUS, 2, &status);
  417. if (ret) {
  418. dev_err(p->dev, "unable to read abort status\n");
  419. return -EINVAL;
  420. }
  421. dev_err(p->dev,
  422. "PCI: abort_handler addr = %#lx, isr = %#x, status = %#x\n",
  423. addr, isr, status);
  424. /* Make sure the Master Abort bit is reset */
  425. ixp4xx_writel(p, IXP4XX_PCI_ISR, IXP4XX_PCI_ISR_PFE);
  426. status |= PCI_STATUS_REC_MASTER_ABORT;
  427. ret = ixp4xx_crp_write_config(p, PCI_STATUS, 2, status);
  428. if (ret)
  429. dev_err(p->dev, "unable to clear abort status bit\n");
  430. /*
  431. * If it was an imprecise abort, then we need to correct the
  432. * return address to be _after_ the instruction.
  433. */
  434. if (fsr & (1 << 10)) {
  435. dev_err(p->dev, "imprecise abort\n");
  436. regs->ARM_pc += 4;
  437. }
  438. return 0;
  439. }
  440. #endif
  441. static int __init ixp4xx_pci_probe(struct platform_device *pdev)
  442. {
  443. struct device *dev = &pdev->dev;
  444. struct device_node *np = dev->of_node;
  445. struct ixp4xx_pci *p;
  446. struct pci_host_bridge *host;
  447. int ret;
  448. u32 val;
  449. phys_addr_t addr;
  450. u32 basereg[4] = {
  451. PCI_BASE_ADDRESS_0,
  452. PCI_BASE_ADDRESS_1,
  453. PCI_BASE_ADDRESS_2,
  454. PCI_BASE_ADDRESS_3,
  455. };
  456. int i;
  457. host = devm_pci_alloc_host_bridge(dev, sizeof(*p));
  458. if (!host)
  459. return -ENOMEM;
  460. host->ops = &ixp4xx_pci_ops;
  461. p = pci_host_bridge_priv(host);
  462. host->sysdata = p;
  463. p->dev = dev;
  464. dev_set_drvdata(dev, p);
  465. /*
  466. * Set up quirk for erratic behaviour in the 42x variant
  467. * when accessing config space.
  468. */
  469. if (of_device_is_compatible(np, "intel,ixp42x-pci")) {
  470. p->errata_hammer = true;
  471. dev_info(dev, "activate hammering errata\n");
  472. }
  473. p->base = devm_platform_ioremap_resource(pdev, 0);
  474. if (IS_ERR(p->base))
  475. return PTR_ERR(p->base);
  476. val = ixp4xx_readl(p, IXP4XX_PCI_CSR);
  477. p->host_mode = !!(val & IXP4XX_PCI_CSR_HOST);
  478. dev_info(dev, "controller is in %s mode\n",
  479. p->host_mode ? "host" : "option");
  480. #ifdef CONFIG_ARM
  481. /* Hook in our fault handler for PCI errors */
  482. ixp4xx_pci_abort_singleton = p;
  483. hook_fault_code(16+6, ixp4xx_pci_abort_handler, SIGBUS, 0,
  484. "imprecise external abort");
  485. #endif
  486. ret = ixp4xx_pci_parse_map_ranges(p);
  487. if (ret)
  488. return ret;
  489. ret = ixp4xx_pci_parse_map_dma_ranges(p);
  490. if (ret)
  491. return ret;
  492. /* This is only configured in host mode */
  493. if (p->host_mode) {
  494. addr = __pa(PAGE_OFFSET);
  495. /* This is a noop (0x00) but explains what is going on */
  496. addr |= PCI_BASE_ADDRESS_SPACE_MEMORY;
  497. for (i = 0; i < 4; i++) {
  498. /* Write this directly into the config space */
  499. ret = ixp4xx_crp_write_config(p, basereg[i], 4, addr);
  500. if (ret)
  501. dev_err(dev, "failed to set up PCI_BASE_ADDRESS_%d\n", i);
  502. else
  503. dev_info(dev, "set PCI_BASE_ADDR_%d to %pa\n", i, &addr);
  504. addr += SZ_16M;
  505. }
  506. /*
  507. * Enable CSR window at 64 MiB to allow PCI masters to continue
  508. * prefetching past the 64 MiB boundary, if all AHB to PCI
  509. * windows are consecutive.
  510. */
  511. ret = ixp4xx_crp_write_config(p, PCI_BASE_ADDRESS_4, 4, addr);
  512. if (ret)
  513. dev_err(dev, "failed to set up PCI_BASE_ADDRESS_4\n");
  514. else
  515. dev_info(dev, "set PCI_BASE_ADDR_4 to %pa\n", &addr);
  516. /*
  517. * Put the IO memory window at the very end of physical memory
  518. * at 0xfffffc00. This is when the system is trying to access IO
  519. * memory over AHB.
  520. */
  521. addr = 0xfffffc00;
  522. addr |= PCI_BASE_ADDRESS_SPACE_IO;
  523. ret = ixp4xx_crp_write_config(p, PCI_BASE_ADDRESS_5, 4, addr);
  524. if (ret)
  525. dev_err(dev, "failed to set up PCI_BASE_ADDRESS_5\n");
  526. else
  527. dev_info(dev, "set PCI_BASE_ADDR_5 to %pa\n", &addr);
  528. /*
  529. * Retry timeout to 0x80
  530. * Transfer ready timeout to 0xff
  531. */
  532. ret = ixp4xx_crp_write_config(p, IXP4XX_PCI_RTOTTO, 4,
  533. 0x000080ff);
  534. if (ret)
  535. dev_err(dev, "failed to set up TRDY limit\n");
  536. else
  537. dev_info(dev, "set TRDY limit to 0x80ff\n");
  538. }
  539. /* Clear interrupts */
  540. val = IXP4XX_PCI_ISR_PSE | IXP4XX_PCI_ISR_PFE | IXP4XX_PCI_ISR_PPE | IXP4XX_PCI_ISR_AHBE;
  541. ixp4xx_writel(p, IXP4XX_PCI_ISR, val);
  542. /*
  543. * Set Initialize Complete in PCI Control Register: allow IXP4XX to
  544. * generate PCI configuration cycles. Specify that the AHB bus is
  545. * operating in big-endian mode. Set up byte lane swapping between
  546. * little-endian PCI and the big-endian AHB bus.
  547. */
  548. val = IXP4XX_PCI_CSR_IC | IXP4XX_PCI_CSR_ABE;
  549. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  550. val |= (IXP4XX_PCI_CSR_PDS | IXP4XX_PCI_CSR_ADS);
  551. ixp4xx_writel(p, IXP4XX_PCI_CSR, val);
  552. ret = ixp4xx_crp_write_config(p, PCI_COMMAND, 2, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
  553. if (ret)
  554. dev_err(dev, "unable to initialize master and command memory\n");
  555. else
  556. dev_info(dev, "initialized as master\n");
  557. pci_host_probe(host);
  558. return 0;
  559. }
  560. static const struct of_device_id ixp4xx_pci_of_match[] = {
  561. {
  562. .compatible = "intel,ixp42x-pci",
  563. },
  564. {
  565. .compatible = "intel,ixp43x-pci",
  566. },
  567. {},
  568. };
  569. /*
  570. * This driver needs to be a builtin module with suppressed bind
  571. * attributes since the probe() is initializing a hard exception
  572. * handler and this can only be done from __init-tagged code
  573. * sections. This module cannot be removed and inserted at all.
  574. */
  575. static struct platform_driver ixp4xx_pci_driver = {
  576. .driver = {
  577. .name = "ixp4xx-pci",
  578. .suppress_bind_attrs = true,
  579. .of_match_table = ixp4xx_pci_of_match,
  580. },
  581. };
  582. builtin_platform_driver_probe(ixp4xx_pci_driver, ixp4xx_pci_probe);