pci-bcm6348.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * BCM6348 PCI Controller Driver
  4. *
  5. * Copyright (C) 2020 Álvaro Fernández Rojas <[email protected]>
  6. * Copyright (C) 2015 Jonas Gorski <[email protected]>
  7. * Copyright (C) 2008 Maxime Bizon <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/kernel.h>
  13. #include <linux/memblock.h>
  14. #include <linux/mm.h>
  15. #include <linux/module.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_pci.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/pci.h>
  22. #include <linux/reset.h>
  23. #include <linux/types.h>
  24. #include <linux/vmalloc.h>
  25. #include "../pci.h"
  26. #define CARDBUS_DUMMY_ID 0x6348
  27. #define CARDBUS_PCI_IDSEL 0x8
  28. #define FAKE_CB_BRIDGE_SLOT 0x1e
  29. #define BCMPCI_REG_TIMERS 0x40
  30. #define REG_TIMER_TRDY_SHIFT 0
  31. #define REG_TIMER_TRDY_MASK (0xff << REG_TIMER_TRDY_SHIFT)
  32. #define REG_TIMER_RETRY_SHIFT 8
  33. #define REG_TIMER_RETRY_MASK (0xff << REG_TIMER_RETRY_SHIFT)
  34. #define MPI_SP0_RANGE_REG 0x100
  35. #define MPI_SP0_REMAP_REG 0x104
  36. #define MPI_SP0_REMAP_ENABLE_MASK (1 << 0)
  37. #define MPI_SP1_RANGE_REG 0x10C
  38. #define MPI_SP1_REMAP_REG 0x110
  39. #define MPI_SP1_REMAP_ENABLE_MASK (1 << 0)
  40. #define MPI_L2PCFG_REG 0x11c
  41. #define MPI_L2PCFG_CFG_TYPE_SHIFT 0
  42. #define MPI_L2PCFG_CFG_TYPE_MASK (0x3 << MPI_L2PCFG_CFG_TYPE_SHIFT)
  43. #define MPI_L2PCFG_REG_SHIFT 2
  44. #define MPI_L2PCFG_REG_MASK (0x3f << MPI_L2PCFG_REG_SHIFT)
  45. #define MPI_L2PCFG_FUNC_SHIFT 8
  46. #define MPI_L2PCFG_FUNC_MASK (0x7 << MPI_L2PCFG_FUNC_SHIFT)
  47. #define MPI_L2PCFG_DEVNUM_SHIFT 11
  48. #define MPI_L2PCFG_DEVNUM_MASK (0x1f << MPI_L2PCFG_DEVNUM_SHIFT)
  49. #define MPI_L2PCFG_CFG_USEREG_MASK (1 << 30)
  50. #define MPI_L2PCFG_CFG_SEL_MASK (1 << 31)
  51. #define MPI_L2PMEMRANGE1_REG 0x120
  52. #define MPI_L2PMEMBASE1_REG 0x124
  53. #define MPI_L2PMEMREMAP1_REG 0x128
  54. #define MPI_L2PMEMRANGE2_REG 0x12C
  55. #define MPI_L2PMEMBASE2_REG 0x130
  56. #define MPI_L2PMEMREMAP2_REG 0x134
  57. #define MPI_L2PIORANGE_REG 0x138
  58. #define MPI_L2PIOBASE_REG 0x13C
  59. #define MPI_L2PIOREMAP_REG 0x140
  60. #define MPI_L2P_BASE_MASK (0xffff8000)
  61. #define MPI_L2PREMAP_ENABLED_MASK (1 << 0)
  62. #define MPI_L2PREMAP_IS_CARDBUS_MASK (1 << 2)
  63. #define MPI_PCIMODESEL_REG 0x144
  64. #define MPI_PCIMODESEL_BAR1_NOSWAP_MASK (1 << 0)
  65. #define MPI_PCIMODESEL_BAR2_NOSWAP_MASK (1 << 1)
  66. #define MPI_PCIMODESEL_EXT_ARB_MASK (1 << 2)
  67. #define MPI_PCIMODESEL_PREFETCH_SHIFT 4
  68. #define MPI_PCIMODESEL_PREFETCH_MASK (0xf << MPI_PCIMODESEL_PREFETCH_SHIFT)
  69. #define MPI_LOCBUSCTL_REG 0x14c
  70. #define MPI_LOCBUSCTL_EN_PCI_GPIO_MASK (1 << 0)
  71. #define MPI_LOCBUSCTL_U2P_NOSWAP_MASK (1 << 1)
  72. #define MPI_LOCINT_REG 0x150
  73. #define MPI_LOCINT_MASK(x) (1 << (x + 16))
  74. #define MPI_LOCINT_STAT(x) (1 << (x))
  75. #define MPI_LOCINT_DIR_FAILED 6
  76. #define MPI_LOCINT_EXT_PCI_INT 7
  77. #define MPI_LOCINT_SERR 8
  78. #define MPI_LOCINT_CSERR 9
  79. #define MPI_PCICFGCTL_REG 0x178
  80. #define MPI_PCICFGCTL_CFGADDR_SHIFT 2
  81. #define MPI_PCICFGCTL_CFGADDR_MASK (0x1f << MPI_PCICFGCTL_CFGADDR_SHIFT)
  82. #define MPI_PCICFGCTL_WRITEEN_MASK (1 << 7)
  83. #define MPI_PCICFGDATA_REG 0x17c
  84. #define PCMCIA_OFFSET 0x54
  85. #define PCMCIA_C1_REG 0x0
  86. #define PCMCIA_C1_CD1_MASK (1 << 0)
  87. #define PCMCIA_C1_CD2_MASK (1 << 1)
  88. #define PCMCIA_C1_VS1_MASK (1 << 2)
  89. #define PCMCIA_C1_VS2_MASK (1 << 3)
  90. #define PCMCIA_C1_VS1OE_MASK (1 << 6)
  91. #define PCMCIA_C1_VS2OE_MASK (1 << 7)
  92. #define PCMCIA_C1_CBIDSEL_SHIFT (8)
  93. #define PCMCIA_C1_CBIDSEL_MASK (0x1f << PCMCIA_C1_CBIDSEL_SHIFT)
  94. #define PCMCIA_C1_EN_PCMCIA_GPIO_MASK (1 << 13)
  95. #define PCMCIA_C1_EN_PCMCIA_MASK (1 << 14)
  96. #define PCMCIA_C1_EN_CARDBUS_MASK (1 << 15)
  97. #define PCMCIA_C1_RESET_MASK (1 << 18)
  98. #ifdef CONFIG_CARDBUS
  99. struct bcm6348_cb {
  100. u16 pci_command;
  101. u8 cb_latency;
  102. u8 subordinate_busn;
  103. u8 cardbus_busn;
  104. u8 pci_busn;
  105. int bus_assigned;
  106. u16 bridge_control;
  107. u32 mem_base0;
  108. u32 mem_limit0;
  109. u32 mem_base1;
  110. u32 mem_limit1;
  111. u32 io_base0;
  112. u32 io_limit0;
  113. u32 io_base1;
  114. u32 io_limit1;
  115. };
  116. #endif /* CONFIG_CARDBUS */
  117. struct bcm6348_pci {
  118. void __iomem *pci;
  119. void __iomem *pcmcia;
  120. void __iomem *io;
  121. int irq;
  122. struct reset_control *reset;
  123. bool remap;
  124. #ifdef CONFIG_CARDBUS
  125. struct bcm6348_cb cb;
  126. int cb_bus;
  127. #endif /* CONFIG_CARDBUS */
  128. };
  129. static struct bcm6348_pci bcm6348_pci;
  130. extern int bmips_pci_irq;
  131. static u32 bcm6348_int_cfg_readl(u32 reg)
  132. {
  133. struct bcm6348_pci *priv = &bcm6348_pci;
  134. u32 tmp;
  135. tmp = reg & MPI_PCICFGCTL_CFGADDR_MASK;
  136. tmp |= MPI_PCICFGCTL_WRITEEN_MASK;
  137. __raw_writel(tmp, priv->pci + MPI_PCICFGCTL_REG);
  138. iob();
  139. return __raw_readl(priv->pci + MPI_PCICFGDATA_REG);
  140. }
  141. static void bcm6348_int_cfg_writel(u32 val, u32 reg)
  142. {
  143. struct bcm6348_pci *priv = &bcm6348_pci;
  144. u32 tmp;
  145. tmp = reg & MPI_PCICFGCTL_CFGADDR_MASK;
  146. tmp |= MPI_PCICFGCTL_WRITEEN_MASK;
  147. __raw_writel(tmp, priv->pci + MPI_PCICFGCTL_REG);
  148. __raw_writel(val, priv->pci + MPI_PCICFGDATA_REG);
  149. }
  150. /*
  151. * swizzle 32bits data to return only the needed part
  152. */
  153. static int postprocess_read(u32 data, int where, unsigned int size)
  154. {
  155. u32 ret = 0;
  156. switch (size) {
  157. case 1:
  158. ret = (data >> ((where & 3) << 3)) & 0xff;
  159. break;
  160. case 2:
  161. ret = (data >> ((where & 3) << 3)) & 0xffff;
  162. break;
  163. case 4:
  164. ret = data;
  165. break;
  166. }
  167. return ret;
  168. }
  169. static int preprocess_write(u32 orig_data, u32 val, int where,
  170. unsigned int size)
  171. {
  172. u32 ret = 0;
  173. switch (size) {
  174. case 1:
  175. ret = (orig_data & ~(0xff << ((where & 3) << 3))) |
  176. (val << ((where & 3) << 3));
  177. break;
  178. case 2:
  179. ret = (orig_data & ~(0xffff << ((where & 3) << 3))) |
  180. (val << ((where & 3) << 3));
  181. break;
  182. case 4:
  183. ret = val;
  184. break;
  185. }
  186. return ret;
  187. }
  188. static int bcm6348_setup_cfg_access(int type, unsigned int busn,
  189. unsigned int devfn, int where)
  190. {
  191. struct bcm6348_pci *priv = &bcm6348_pci;
  192. unsigned int slot, func, reg;
  193. u32 val;
  194. slot = PCI_SLOT(devfn);
  195. func = PCI_FUNC(devfn);
  196. reg = where >> 2;
  197. /* sanity check */
  198. if (slot > (MPI_L2PCFG_DEVNUM_MASK >> MPI_L2PCFG_DEVNUM_SHIFT))
  199. return 1;
  200. if (func > (MPI_L2PCFG_FUNC_MASK >> MPI_L2PCFG_FUNC_SHIFT))
  201. return 1;
  202. if (reg > (MPI_L2PCFG_REG_MASK >> MPI_L2PCFG_REG_SHIFT))
  203. return 1;
  204. /* ok, setup config access */
  205. val = (reg << MPI_L2PCFG_REG_SHIFT);
  206. val |= (func << MPI_L2PCFG_FUNC_SHIFT);
  207. val |= (slot << MPI_L2PCFG_DEVNUM_SHIFT);
  208. val |= MPI_L2PCFG_CFG_USEREG_MASK;
  209. val |= MPI_L2PCFG_CFG_SEL_MASK;
  210. /* type 0 cycle for local bus, type 1 cycle for anything else */
  211. if (type != 0) {
  212. /* FIXME: how to specify bus ??? */
  213. val |= (1 << MPI_L2PCFG_CFG_TYPE_SHIFT);
  214. }
  215. __raw_writel(val, priv->pci + MPI_L2PCFG_REG);
  216. return 0;
  217. }
  218. static int bcm6348_do_cfg_read(int type, unsigned int busn,
  219. unsigned int devfn, int where, int size,
  220. u32 *val)
  221. {
  222. struct bcm6348_pci *priv = &bcm6348_pci;
  223. u32 data;
  224. /* two phase cycle, first we write address, then read data at
  225. * another location, caller already has a spinlock so no need
  226. * to add one here */
  227. if (bcm6348_setup_cfg_access(type, busn, devfn, where))
  228. return PCIBIOS_DEVICE_NOT_FOUND;
  229. iob();
  230. data = le32_to_cpu(__raw_readl(priv->io));
  231. /* restore IO space normal behaviour */
  232. __raw_writel(0, priv->pci + MPI_L2PCFG_REG);
  233. *val = postprocess_read(data, where, size);
  234. return PCIBIOS_SUCCESSFUL;
  235. }
  236. static int bcm6348_do_cfg_write(int type, unsigned int busn,
  237. unsigned int devfn, int where, int size,
  238. u32 val)
  239. {
  240. struct bcm6348_pci *priv = &bcm6348_pci;
  241. u32 data;
  242. /* two phase cycle, first we write address, then write data to
  243. * another location, caller already has a spinlock so no need
  244. * to add one here */
  245. if (bcm6348_setup_cfg_access(type, busn, devfn, where))
  246. return PCIBIOS_DEVICE_NOT_FOUND;
  247. iob();
  248. data = le32_to_cpu(__raw_readl(priv->io));
  249. data = preprocess_write(data, val, where, size);
  250. __raw_writel(cpu_to_le32(data), priv->io);
  251. wmb();
  252. /* no way to know the access is done, we have to wait */
  253. udelay(500);
  254. /* restore IO space normal behaviour */
  255. __raw_writel(0, priv->pci + MPI_L2PCFG_REG);
  256. return PCIBIOS_SUCCESSFUL;
  257. }
  258. static int bcm6348_pci_read(struct pci_bus *bus, unsigned int devfn,
  259. int where, int size, u32 *val)
  260. {
  261. int type;
  262. type = bus->parent ? 1 : 0;
  263. if (type == 0 && PCI_SLOT(devfn) == CARDBUS_PCI_IDSEL)
  264. return PCIBIOS_DEVICE_NOT_FOUND;
  265. return bcm6348_do_cfg_read(type, bus->number, devfn,
  266. where, size, val);
  267. }
  268. static int bcm6348_pci_write(struct pci_bus *bus, unsigned int devfn,
  269. int where, int size, u32 val)
  270. {
  271. int type;
  272. type = bus->parent ? 1 : 0;
  273. if (type == 0 && PCI_SLOT(devfn) == CARDBUS_PCI_IDSEL)
  274. return PCIBIOS_DEVICE_NOT_FOUND;
  275. return bcm6348_do_cfg_write(type, bus->number, devfn,
  276. where, size, val);
  277. }
  278. static struct pci_ops bcm6348_pci_ops = {
  279. .read = bcm6348_pci_read,
  280. .write = bcm6348_pci_write,
  281. };
  282. static struct resource bcm6348_pci_io_resource;
  283. static struct resource bcm6348_pci_mem_resource;
  284. static struct resource bcm6348_pci_busn_resource;
  285. static struct pci_controller bcm6348_pci_controller = {
  286. .pci_ops = &bcm6348_pci_ops,
  287. .io_resource = &bcm6348_pci_io_resource,
  288. .mem_resource = &bcm6348_pci_mem_resource,
  289. };
  290. #ifdef CONFIG_CARDBUS
  291. static int bcm6348_cb_bridge_read(int where, int size, u32 *val)
  292. {
  293. struct bcm6348_cb *cb = &bcm6348_pci.cb;
  294. unsigned int reg;
  295. u32 data;
  296. data = 0;
  297. reg = where >> 2;
  298. switch (reg) {
  299. case (PCI_VENDOR_ID >> 2):
  300. case (PCI_CB_SUBSYSTEM_VENDOR_ID >> 2):
  301. /* create dummy vendor/device id from our cpu id */
  302. data = (CARDBUS_DUMMY_ID << 16) | PCI_VENDOR_ID_BROADCOM;
  303. break;
  304. case (PCI_COMMAND >> 2):
  305. data = (PCI_STATUS_DEVSEL_SLOW << 16);
  306. data |= cb->pci_command;
  307. break;
  308. case (PCI_CLASS_REVISION >> 2):
  309. data = (PCI_CLASS_BRIDGE_CARDBUS << 16);
  310. break;
  311. case (PCI_CACHE_LINE_SIZE >> 2):
  312. data = (PCI_HEADER_TYPE_CARDBUS << 16);
  313. break;
  314. case (PCI_INTERRUPT_LINE >> 2):
  315. /* bridge control */
  316. data = (cb->bridge_control << 16);
  317. /* pin:intA line:0xff */
  318. data |= (0x1 << 8) | 0xff;
  319. break;
  320. case (PCI_CB_PRIMARY_BUS >> 2):
  321. data = (cb->cb_latency << 24);
  322. data |= (cb->subordinate_busn << 16);
  323. data |= (cb->cardbus_busn << 8);
  324. data |= cb->pci_busn;
  325. break;
  326. case (PCI_CB_MEMORY_BASE_0 >> 2):
  327. data = cb->mem_base0;
  328. break;
  329. case (PCI_CB_MEMORY_LIMIT_0 >> 2):
  330. data = cb->mem_limit0;
  331. break;
  332. case (PCI_CB_MEMORY_BASE_1 >> 2):
  333. data = cb->mem_base1;
  334. break;
  335. case (PCI_CB_MEMORY_LIMIT_1 >> 2):
  336. data = cb->mem_limit1;
  337. break;
  338. case (PCI_CB_IO_BASE_0 >> 2):
  339. /* | 1 for 32bits io support */
  340. data = cb->io_base0 | 0x1;
  341. break;
  342. case (PCI_CB_IO_LIMIT_0 >> 2):
  343. data = cb->io_limit0;
  344. break;
  345. case (PCI_CB_IO_BASE_1 >> 2):
  346. /* | 1 for 32bits io support */
  347. data = cb->io_base1 | 0x1;
  348. break;
  349. case (PCI_CB_IO_LIMIT_1 >> 2):
  350. data = cb->io_limit1;
  351. break;
  352. }
  353. *val = postprocess_read(data, where, size);
  354. return PCIBIOS_SUCCESSFUL;
  355. }
  356. /*
  357. * emulate configuration write access on a cardbus bridge
  358. */
  359. static int bcm6348_cb_bridge_write(int where, int size, u32 val)
  360. {
  361. struct bcm6348_cb *cb = &bcm6348_pci.cb;
  362. unsigned int reg;
  363. u32 data, tmp;
  364. int ret;
  365. ret = bcm6348_cb_bridge_read((where & ~0x3), 4, &data);
  366. if (ret != PCIBIOS_SUCCESSFUL)
  367. return ret;
  368. data = preprocess_write(data, val, where, size);
  369. reg = where >> 2;
  370. switch (reg) {
  371. case (PCI_COMMAND >> 2):
  372. cb->pci_command = (data & 0xffff);
  373. break;
  374. case (PCI_CB_PRIMARY_BUS >> 2):
  375. cb->cb_latency = (data >> 24) & 0xff;
  376. cb->subordinate_busn = (data >> 16) & 0xff;
  377. cb->cardbus_busn = (data >> 8) & 0xff;
  378. cb->pci_busn = data & 0xff;
  379. if (cb->cardbus_busn)
  380. cb->bus_assigned = 1;
  381. break;
  382. case (PCI_INTERRUPT_LINE >> 2):
  383. tmp = (data >> 16) & 0xffff;
  384. /* Disable memory prefetch support */
  385. tmp &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  386. tmp &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
  387. cb->bridge_control = tmp;
  388. break;
  389. case (PCI_CB_MEMORY_BASE_0 >> 2):
  390. cb->mem_base0 = data;
  391. break;
  392. case (PCI_CB_MEMORY_LIMIT_0 >> 2):
  393. cb->mem_limit0 = data;
  394. break;
  395. case (PCI_CB_MEMORY_BASE_1 >> 2):
  396. cb->mem_base1 = data;
  397. break;
  398. case (PCI_CB_MEMORY_LIMIT_1 >> 2):
  399. cb->mem_limit1 = data;
  400. break;
  401. case (PCI_CB_IO_BASE_0 >> 2):
  402. cb->io_base0 = data;
  403. break;
  404. case (PCI_CB_IO_LIMIT_0 >> 2):
  405. cb->io_limit0 = data;
  406. break;
  407. case (PCI_CB_IO_BASE_1 >> 2):
  408. cb->io_base1 = data;
  409. break;
  410. case (PCI_CB_IO_LIMIT_1 >> 2):
  411. cb->io_limit1 = data;
  412. break;
  413. }
  414. return PCIBIOS_SUCCESSFUL;
  415. }
  416. static int bcm6348_cb_read(struct pci_bus *bus, unsigned int devfn,
  417. int where, int size, u32 *val)
  418. {
  419. struct bcm6348_pci *priv = &bcm6348_pci;
  420. struct bcm6348_cb *cb = &priv->cb;
  421. /* Snoop access to slot 0x1e on root bus, we fake a cardbus
  422. * bridge at this location */
  423. if (!bus->parent && PCI_SLOT(devfn) == FAKE_CB_BRIDGE_SLOT) {
  424. priv->cb_bus = bus->number;
  425. return bcm6348_cb_bridge_read(where, size, val);
  426. }
  427. /* A configuration cycle for the device behind the cardbus
  428. * bridge is actually done as a type 0 cycle on the primary
  429. * bus. This means that only one device can be on the cardbus
  430. * bus */
  431. if (cb->bus_assigned &&
  432. bus->number == cb->cardbus_busn &&
  433. PCI_SLOT(devfn) == 0)
  434. return bcm6348_do_cfg_read(0, 0,
  435. PCI_DEVFN(CARDBUS_PCI_IDSEL, 0),
  436. where, size, val);
  437. return PCIBIOS_DEVICE_NOT_FOUND;
  438. }
  439. static int bcm6348_cb_write(struct pci_bus *bus, unsigned int devfn,
  440. int where, int size, u32 val)
  441. {
  442. struct bcm6348_pci *priv = &bcm6348_pci;
  443. struct bcm6348_cb *cb = &priv->cb;
  444. if (!bus->parent && PCI_SLOT(devfn) == FAKE_CB_BRIDGE_SLOT) {
  445. priv->cb_bus = bus->number;
  446. return bcm6348_cb_bridge_write(where, size, val);
  447. }
  448. if (cb->bus_assigned &&
  449. bus->number == cb->cardbus_busn &&
  450. PCI_SLOT(devfn) == 0)
  451. return bcm6348_do_cfg_write(0, 0,
  452. PCI_DEVFN(CARDBUS_PCI_IDSEL, 0),
  453. where, size, val);
  454. return PCIBIOS_DEVICE_NOT_FOUND;
  455. }
  456. static struct pci_ops bcm6348_cb_ops = {
  457. .read = bcm6348_cb_read,
  458. .write = bcm6348_cb_write,
  459. };
  460. /*
  461. * only one IO window, so it cannot be shared by PCI and cardbus, use
  462. * fixup to choose and detect unhandled configuration
  463. */
  464. static void bcm6348_pci_fixup(struct pci_dev *dev)
  465. {
  466. struct bcm6348_pci *priv = &bcm6348_pci;
  467. struct bcm6348_cb *cb = &priv->cb;
  468. static int io_window = -1;
  469. int i, found, new_io_window;
  470. u32 val;
  471. /* look for any io resource */
  472. found = 0;
  473. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  474. if (pci_resource_flags(dev, i) & IORESOURCE_IO) {
  475. found = 1;
  476. break;
  477. }
  478. }
  479. if (!found)
  480. return;
  481. /* skip our fake bus with only cardbus bridge on it */
  482. if (dev->bus->number == priv->cb_bus)
  483. return;
  484. /* find on which bus the device is */
  485. if (cb->bus_assigned &&
  486. dev->bus->number == cb->cardbus_busn &&
  487. PCI_SLOT(dev->devfn) == 0)
  488. new_io_window = 1;
  489. else
  490. new_io_window = 0;
  491. if (new_io_window == io_window)
  492. return;
  493. if (io_window != -1) {
  494. pr_err("bcm63xx: both PCI and cardbus devices "
  495. "need IO, which hardware cannot do\n");
  496. return;
  497. }
  498. pr_info("bcm63xx: PCI IO window assigned to %s\n",
  499. (new_io_window == 0) ? "PCI" : "cardbus");
  500. val = __raw_readl(priv->pci + MPI_L2PIOREMAP_REG);
  501. if (io_window)
  502. val |= MPI_L2PREMAP_IS_CARDBUS_MASK;
  503. else
  504. val &= ~MPI_L2PREMAP_IS_CARDBUS_MASK;
  505. __raw_writel(val, priv->pci + MPI_L2PIOREMAP_REG);
  506. io_window = new_io_window;
  507. }
  508. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, bcm6348_pci_fixup);
  509. static struct resource bcm6348_cb_io_resource = {
  510. .name = "bcm6348 CB IO space",
  511. .flags = IORESOURCE_IO,
  512. };
  513. static struct resource bcm6348_cb_mem_resource;
  514. static struct pci_controller bcm6348_cb_controller = {
  515. .pci_ops = &bcm6348_cb_ops,
  516. .io_resource = &bcm6348_cb_io_resource,
  517. .mem_resource = &bcm6348_cb_mem_resource,
  518. };
  519. #endif /* CONFIG_CARDBUS */
  520. static void bcm6348_pci_setup(struct bcm6348_pci *priv)
  521. {
  522. u32 val;
  523. /* Setup local bus to PCI access (PCI memory) */
  524. val = bcm6348_pci_mem_resource.start & MPI_L2P_BASE_MASK;
  525. __raw_writel(val, priv->pci + MPI_L2PMEMBASE1_REG);
  526. __raw_writel(~(resource_size(&bcm6348_pci_mem_resource) - 1),
  527. priv->pci + MPI_L2PMEMRANGE1_REG);
  528. __raw_writel(val | MPI_L2PREMAP_ENABLED_MASK,
  529. priv->pci + MPI_L2PMEMREMAP1_REG);
  530. /* Set Cardbus IDSEL (type 0 cfg access on primary bus for
  531. * this IDSEL will be done on Cardbus instead) */
  532. val = __raw_readl(priv->pcmcia + PCMCIA_C1_REG);
  533. val &= ~PCMCIA_C1_CBIDSEL_MASK;
  534. val |= (CARDBUS_PCI_IDSEL << PCMCIA_C1_CBIDSEL_SHIFT);
  535. __raw_writel(val, priv->pcmcia + PCMCIA_C1_REG);
  536. #ifdef CONFIG_CARDBUS
  537. /* setup local bus to PCI access (Cardbus memory) */
  538. val = bcm6348_cb_mem_resource.start & MPI_L2P_BASE_MASK;
  539. __raw_writel(val, priv->pci + MPI_L2PMEMBASE2_REG);
  540. __raw_writel(~(resource_size(&bcm6348_cb_mem_resource) - 1),
  541. priv->pci + MPI_L2PMEMRANGE2_REG);
  542. val |= MPI_L2PREMAP_ENABLED_MASK | MPI_L2PREMAP_IS_CARDBUS_MASK;
  543. __raw_writel(val, priv->pci + MPI_L2PMEMREMAP2_REG);
  544. #else
  545. /* disable second access windows */
  546. __raw_writel(0, priv->pci + MPI_L2PMEMREMAP2_REG);
  547. #endif
  548. /* setup local bus to PCI access (IO memory), we have only 1
  549. * IO window for both PCI and cardbus, but it cannot handle
  550. * both at the same time, assume standard PCI for now, if
  551. * cardbus card has IO zone, PCI fixup will change window to
  552. * cardbus */
  553. val = bcm6348_pci_io_resource.start & MPI_L2P_BASE_MASK;
  554. __raw_writel(val, priv->pci + MPI_L2PIOBASE_REG);
  555. __raw_writel(~(resource_size(&bcm6348_pci_io_resource) - 1),
  556. priv->pci + MPI_L2PIORANGE_REG);
  557. __raw_writel(val | MPI_L2PREMAP_ENABLED_MASK,
  558. priv->pci + MPI_L2PIOREMAP_REG);
  559. /* Enable PCI related GPIO pins */
  560. __raw_writel(MPI_LOCBUSCTL_EN_PCI_GPIO_MASK,
  561. priv->pci + MPI_LOCBUSCTL_REG);
  562. /* Setup PCI to local bus access, used by PCI device to target
  563. * local RAM while bus mastering */
  564. bcm6348_int_cfg_writel(0, PCI_BASE_ADDRESS_3);
  565. if (priv->remap)
  566. val = MPI_SP0_REMAP_ENABLE_MASK;
  567. else
  568. val = 0;
  569. __raw_writel(val, priv->pci + MPI_SP0_REMAP_REG);
  570. bcm6348_int_cfg_writel(0, PCI_BASE_ADDRESS_4);
  571. __raw_writel(0, priv->pci + MPI_SP1_REMAP_REG);
  572. /* Setup sp0 range to local RAM size */
  573. __raw_writel(~(memblock_phys_mem_size() - 1),
  574. priv->pci + MPI_SP0_RANGE_REG);
  575. __raw_writel(0, priv->pci + MPI_SP1_RANGE_REG);
  576. /* Change host bridge retry counter to infinite number of
  577. * retries, needed for some broadcom wifi cards with Silicon
  578. * Backplane bus where access to srom seems very slow */
  579. val = bcm6348_int_cfg_readl(BCMPCI_REG_TIMERS);
  580. val &= ~REG_TIMER_RETRY_MASK;
  581. bcm6348_int_cfg_writel(val, BCMPCI_REG_TIMERS);
  582. /* EEnable memory decoder and bus mastering */
  583. val = bcm6348_int_cfg_readl(PCI_COMMAND);
  584. val |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  585. bcm6348_int_cfg_writel(val, PCI_COMMAND);
  586. /* Enable read prefetching & disable byte swapping for bus
  587. * mastering transfers */
  588. val = __raw_readl(priv->pci + MPI_PCIMODESEL_REG);
  589. val &= ~MPI_PCIMODESEL_BAR1_NOSWAP_MASK;
  590. val &= ~MPI_PCIMODESEL_BAR2_NOSWAP_MASK;
  591. val &= ~MPI_PCIMODESEL_PREFETCH_MASK;
  592. val |= (8 << MPI_PCIMODESEL_PREFETCH_SHIFT);
  593. __raw_writel(val, priv->pci + MPI_PCIMODESEL_REG);
  594. /* Enable pci interrupt */
  595. val = __raw_readl(priv->pci + MPI_LOCINT_REG);
  596. val |= MPI_LOCINT_MASK(MPI_LOCINT_EXT_PCI_INT);
  597. __raw_writel(val, priv->pci + MPI_LOCINT_REG);
  598. }
  599. static int bcm6348_pci_probe(struct platform_device *pdev)
  600. {
  601. struct device *dev = &pdev->dev;
  602. struct device_node *np = dev->of_node;
  603. struct bcm6348_pci *priv = &bcm6348_pci;
  604. struct resource *res;
  605. LIST_HEAD(resources);
  606. of_pci_check_probe_only();
  607. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  608. priv->pci = devm_ioremap_resource(dev, res);
  609. if (IS_ERR(priv->pci))
  610. return PTR_ERR(priv->pci);
  611. priv->pcmcia = priv->pci + PCMCIA_OFFSET;
  612. priv->irq = platform_get_irq(pdev, 0);
  613. if (!priv->irq)
  614. return -ENODEV;
  615. bmips_pci_irq = priv->irq;
  616. priv->reset = devm_reset_control_get(dev, "pci");
  617. if (IS_ERR(priv->reset))
  618. return PTR_ERR(priv->reset);
  619. priv->remap = of_property_read_bool(np, "brcm,remap");
  620. reset_control_reset(priv->reset);
  621. pci_load_of_ranges(&bcm6348_pci_controller, np);
  622. if (!bcm6348_pci_mem_resource.start)
  623. return -EINVAL;
  624. of_pci_parse_bus_range(np, &bcm6348_pci_busn_resource);
  625. pci_add_resource(&resources, &bcm6348_pci_busn_resource);
  626. #ifdef CONFIG_CARDBUS
  627. bcm6348_cb_io_resource.start = bcm6348_pci_io_resource.start + (resource_size(&bcm6348_pci_io_resource) >> 1);
  628. bcm6348_cb_io_resource.end = bcm6348_pci_io_resource.end;
  629. bcm6348_pci_io_resource.end = bcm6348_pci_io_resource.end - (resource_size(&bcm6348_pci_io_resource) >> 1);
  630. #endif
  631. /*
  632. * Configuration accesses are done through IO space, remap 4
  633. * first bytes to access it from CPU.
  634. *
  635. * This means that no IO access from CPU should happen while
  636. * we do a configuration cycle, but there's no way we can add
  637. * a spinlock for each io access, so this is currently kind of
  638. * broken on SMP.
  639. */
  640. priv->io = ioremap(bcm6348_pci_io_resource.start, sizeof(u32));
  641. if (!priv->io)
  642. return -ENOMEM;
  643. bcm6348_pci_setup(priv);
  644. register_pci_controller(&bcm6348_pci_controller);
  645. #ifdef CONFIG_CARDBUS
  646. priv->cb_bus = -1;
  647. register_pci_controller(&bcm6348_cb_controller);
  648. #endif /* CONFIG_CARDBUS */
  649. /* Mark memory space used for IO mapping as reserved */
  650. request_mem_region(bcm6348_pci_io_resource.start,
  651. resource_size(&bcm6348_pci_io_resource),
  652. "BCM6348 PCI IO space");
  653. return 0;
  654. }
  655. static const struct of_device_id bcm6348_pci_of_match[] = {
  656. { .compatible = "brcm,bcm6348-pci", },
  657. { /* sentinel */ }
  658. };
  659. MODULE_DEVICE_TABLE(of, bcm6348_pci_of_match);
  660. static struct platform_driver bcm6348_pci_driver = {
  661. .probe = bcm6348_pci_probe,
  662. .driver = {
  663. .name = "bcm6348-pci",
  664. .of_match_table = bcm6348_pci_of_match,
  665. },
  666. };
  667. module_platform_driver(bcm6348_pci_driver);
  668. MODULE_AUTHOR("Álvaro Fernández Rojas <[email protected]>");
  669. MODULE_DESCRIPTION("BCM6348 PCI Controller Driver");
  670. MODULE_LICENSE("GPL v2");
  671. MODULE_ALIAS("platform:bcm6348-pci");