ops-bcm63xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include "pci-bcm63xx.h"
  14. /*
  15. * swizzle 32bits data to return only the needed part
  16. */
  17. static int postprocess_read(u32 data, int where, unsigned int size)
  18. {
  19. u32 ret;
  20. ret = 0;
  21. switch (size) {
  22. case 1:
  23. ret = (data >> ((where & 3) << 3)) & 0xff;
  24. break;
  25. case 2:
  26. ret = (data >> ((where & 3) << 3)) & 0xffff;
  27. break;
  28. case 4:
  29. ret = data;
  30. break;
  31. }
  32. return ret;
  33. }
  34. static int preprocess_write(u32 orig_data, u32 val, int where,
  35. unsigned int size)
  36. {
  37. u32 ret;
  38. ret = 0;
  39. switch (size) {
  40. case 1:
  41. ret = (orig_data & ~(0xff << ((where & 3) << 3))) |
  42. (val << ((where & 3) << 3));
  43. break;
  44. case 2:
  45. ret = (orig_data & ~(0xffff << ((where & 3) << 3))) |
  46. (val << ((where & 3) << 3));
  47. break;
  48. case 4:
  49. ret = val;
  50. break;
  51. }
  52. return ret;
  53. }
  54. /*
  55. * setup hardware for a configuration cycle with given parameters
  56. */
  57. static int bcm63xx_setup_cfg_access(int type, unsigned int busn,
  58. unsigned int devfn, int where)
  59. {
  60. unsigned int slot, func, reg;
  61. u32 val;
  62. slot = PCI_SLOT(devfn);
  63. func = PCI_FUNC(devfn);
  64. reg = where >> 2;
  65. /* sanity check */
  66. if (slot > (MPI_L2PCFG_DEVNUM_MASK >> MPI_L2PCFG_DEVNUM_SHIFT))
  67. return 1;
  68. if (func > (MPI_L2PCFG_FUNC_MASK >> MPI_L2PCFG_FUNC_SHIFT))
  69. return 1;
  70. if (reg > (MPI_L2PCFG_REG_MASK >> MPI_L2PCFG_REG_SHIFT))
  71. return 1;
  72. /* ok, setup config access */
  73. val = (reg << MPI_L2PCFG_REG_SHIFT);
  74. val |= (func << MPI_L2PCFG_FUNC_SHIFT);
  75. val |= (slot << MPI_L2PCFG_DEVNUM_SHIFT);
  76. val |= MPI_L2PCFG_CFG_USEREG_MASK;
  77. val |= MPI_L2PCFG_CFG_SEL_MASK;
  78. /* type 0 cycle for local bus, type 1 cycle for anything else */
  79. if (type != 0) {
  80. /* FIXME: how to specify bus ??? */
  81. val |= (1 << MPI_L2PCFG_CFG_TYPE_SHIFT);
  82. }
  83. bcm_mpi_writel(val, MPI_L2PCFG_REG);
  84. return 0;
  85. }
  86. static int bcm63xx_do_cfg_read(int type, unsigned int busn,
  87. unsigned int devfn, int where, int size,
  88. u32 *val)
  89. {
  90. u32 data;
  91. /* two phase cycle, first we write address, then read data at
  92. * another location, caller already has a spinlock so no need
  93. * to add one here */
  94. if (bcm63xx_setup_cfg_access(type, busn, devfn, where))
  95. return PCIBIOS_DEVICE_NOT_FOUND;
  96. iob();
  97. data = le32_to_cpu(__raw_readl(pci_iospace_start));
  98. /* restore IO space normal behaviour */
  99. bcm_mpi_writel(0, MPI_L2PCFG_REG);
  100. *val = postprocess_read(data, where, size);
  101. return PCIBIOS_SUCCESSFUL;
  102. }
  103. static int bcm63xx_do_cfg_write(int type, unsigned int busn,
  104. unsigned int devfn, int where, int size,
  105. u32 val)
  106. {
  107. u32 data;
  108. /* two phase cycle, first we write address, then write data to
  109. * another location, caller already has a spinlock so no need
  110. * to add one here */
  111. if (bcm63xx_setup_cfg_access(type, busn, devfn, where))
  112. return PCIBIOS_DEVICE_NOT_FOUND;
  113. iob();
  114. data = le32_to_cpu(__raw_readl(pci_iospace_start));
  115. data = preprocess_write(data, val, where, size);
  116. __raw_writel(cpu_to_le32(data), pci_iospace_start);
  117. wmb();
  118. /* no way to know the access is done, we have to wait */
  119. udelay(500);
  120. /* restore IO space normal behaviour */
  121. bcm_mpi_writel(0, MPI_L2PCFG_REG);
  122. return PCIBIOS_SUCCESSFUL;
  123. }
  124. static int bcm63xx_pci_read(struct pci_bus *bus, unsigned int devfn,
  125. int where, int size, u32 *val)
  126. {
  127. int type;
  128. type = bus->parent ? 1 : 0;
  129. if (type == 0 && PCI_SLOT(devfn) == CARDBUS_PCI_IDSEL)
  130. return PCIBIOS_DEVICE_NOT_FOUND;
  131. return bcm63xx_do_cfg_read(type, bus->number, devfn,
  132. where, size, val);
  133. }
  134. static int bcm63xx_pci_write(struct pci_bus *bus, unsigned int devfn,
  135. int where, int size, u32 val)
  136. {
  137. int type;
  138. type = bus->parent ? 1 : 0;
  139. if (type == 0 && PCI_SLOT(devfn) == CARDBUS_PCI_IDSEL)
  140. return PCIBIOS_DEVICE_NOT_FOUND;
  141. return bcm63xx_do_cfg_write(type, bus->number, devfn,
  142. where, size, val);
  143. }
  144. struct pci_ops bcm63xx_pci_ops = {
  145. .read = bcm63xx_pci_read,
  146. .write = bcm63xx_pci_write
  147. };
  148. #ifdef CONFIG_CARDBUS
  149. /*
  150. * emulate configuration read access on a cardbus bridge
  151. */
  152. #define FAKE_CB_BRIDGE_SLOT 0x1e
  153. static int fake_cb_bridge_bus_number = -1;
  154. static struct {
  155. u16 pci_command;
  156. u8 cb_latency;
  157. u8 subordinate_busn;
  158. u8 cardbus_busn;
  159. u8 pci_busn;
  160. int bus_assigned;
  161. u16 bridge_control;
  162. u32 mem_base0;
  163. u32 mem_limit0;
  164. u32 mem_base1;
  165. u32 mem_limit1;
  166. u32 io_base0;
  167. u32 io_limit0;
  168. u32 io_base1;
  169. u32 io_limit1;
  170. } fake_cb_bridge_regs;
  171. static int fake_cb_bridge_read(int where, int size, u32 *val)
  172. {
  173. unsigned int reg;
  174. u32 data;
  175. data = 0;
  176. reg = where >> 2;
  177. switch (reg) {
  178. case (PCI_VENDOR_ID >> 2):
  179. case (PCI_CB_SUBSYSTEM_VENDOR_ID >> 2):
  180. /* create dummy vendor/device id from our cpu id */
  181. data = (bcm63xx_get_cpu_id() << 16) | PCI_VENDOR_ID_BROADCOM;
  182. break;
  183. case (PCI_COMMAND >> 2):
  184. data = (PCI_STATUS_DEVSEL_SLOW << 16);
  185. data |= fake_cb_bridge_regs.pci_command;
  186. break;
  187. case (PCI_CLASS_REVISION >> 2):
  188. data = (PCI_CLASS_BRIDGE_CARDBUS << 16);
  189. break;
  190. case (PCI_CACHE_LINE_SIZE >> 2):
  191. data = (PCI_HEADER_TYPE_CARDBUS << 16);
  192. break;
  193. case (PCI_INTERRUPT_LINE >> 2):
  194. /* bridge control */
  195. data = (fake_cb_bridge_regs.bridge_control << 16);
  196. /* pin:intA line:0xff */
  197. data |= (0x1 << 8) | 0xff;
  198. break;
  199. case (PCI_CB_PRIMARY_BUS >> 2):
  200. data = (fake_cb_bridge_regs.cb_latency << 24);
  201. data |= (fake_cb_bridge_regs.subordinate_busn << 16);
  202. data |= (fake_cb_bridge_regs.cardbus_busn << 8);
  203. data |= fake_cb_bridge_regs.pci_busn;
  204. break;
  205. case (PCI_CB_MEMORY_BASE_0 >> 2):
  206. data = fake_cb_bridge_regs.mem_base0;
  207. break;
  208. case (PCI_CB_MEMORY_LIMIT_0 >> 2):
  209. data = fake_cb_bridge_regs.mem_limit0;
  210. break;
  211. case (PCI_CB_MEMORY_BASE_1 >> 2):
  212. data = fake_cb_bridge_regs.mem_base1;
  213. break;
  214. case (PCI_CB_MEMORY_LIMIT_1 >> 2):
  215. data = fake_cb_bridge_regs.mem_limit1;
  216. break;
  217. case (PCI_CB_IO_BASE_0 >> 2):
  218. /* | 1 for 32bits io support */
  219. data = fake_cb_bridge_regs.io_base0 | 0x1;
  220. break;
  221. case (PCI_CB_IO_LIMIT_0 >> 2):
  222. data = fake_cb_bridge_regs.io_limit0;
  223. break;
  224. case (PCI_CB_IO_BASE_1 >> 2):
  225. /* | 1 for 32bits io support */
  226. data = fake_cb_bridge_regs.io_base1 | 0x1;
  227. break;
  228. case (PCI_CB_IO_LIMIT_1 >> 2):
  229. data = fake_cb_bridge_regs.io_limit1;
  230. break;
  231. }
  232. *val = postprocess_read(data, where, size);
  233. return PCIBIOS_SUCCESSFUL;
  234. }
  235. /*
  236. * emulate configuration write access on a cardbus bridge
  237. */
  238. static int fake_cb_bridge_write(int where, int size, u32 val)
  239. {
  240. unsigned int reg;
  241. u32 data, tmp;
  242. int ret;
  243. ret = fake_cb_bridge_read((where & ~0x3), 4, &data);
  244. if (ret != PCIBIOS_SUCCESSFUL)
  245. return ret;
  246. data = preprocess_write(data, val, where, size);
  247. reg = where >> 2;
  248. switch (reg) {
  249. case (PCI_COMMAND >> 2):
  250. fake_cb_bridge_regs.pci_command = (data & 0xffff);
  251. break;
  252. case (PCI_CB_PRIMARY_BUS >> 2):
  253. fake_cb_bridge_regs.cb_latency = (data >> 24) & 0xff;
  254. fake_cb_bridge_regs.subordinate_busn = (data >> 16) & 0xff;
  255. fake_cb_bridge_regs.cardbus_busn = (data >> 8) & 0xff;
  256. fake_cb_bridge_regs.pci_busn = data & 0xff;
  257. if (fake_cb_bridge_regs.cardbus_busn)
  258. fake_cb_bridge_regs.bus_assigned = 1;
  259. break;
  260. case (PCI_INTERRUPT_LINE >> 2):
  261. tmp = (data >> 16) & 0xffff;
  262. /* disable memory prefetch support */
  263. tmp &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  264. tmp &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
  265. fake_cb_bridge_regs.bridge_control = tmp;
  266. break;
  267. case (PCI_CB_MEMORY_BASE_0 >> 2):
  268. fake_cb_bridge_regs.mem_base0 = data;
  269. break;
  270. case (PCI_CB_MEMORY_LIMIT_0 >> 2):
  271. fake_cb_bridge_regs.mem_limit0 = data;
  272. break;
  273. case (PCI_CB_MEMORY_BASE_1 >> 2):
  274. fake_cb_bridge_regs.mem_base1 = data;
  275. break;
  276. case (PCI_CB_MEMORY_LIMIT_1 >> 2):
  277. fake_cb_bridge_regs.mem_limit1 = data;
  278. break;
  279. case (PCI_CB_IO_BASE_0 >> 2):
  280. fake_cb_bridge_regs.io_base0 = data;
  281. break;
  282. case (PCI_CB_IO_LIMIT_0 >> 2):
  283. fake_cb_bridge_regs.io_limit0 = data;
  284. break;
  285. case (PCI_CB_IO_BASE_1 >> 2):
  286. fake_cb_bridge_regs.io_base1 = data;
  287. break;
  288. case (PCI_CB_IO_LIMIT_1 >> 2):
  289. fake_cb_bridge_regs.io_limit1 = data;
  290. break;
  291. }
  292. return PCIBIOS_SUCCESSFUL;
  293. }
  294. static int bcm63xx_cb_read(struct pci_bus *bus, unsigned int devfn,
  295. int where, int size, u32 *val)
  296. {
  297. /* snoop access to slot 0x1e on root bus, we fake a cardbus
  298. * bridge at this location */
  299. if (!bus->parent && PCI_SLOT(devfn) == FAKE_CB_BRIDGE_SLOT) {
  300. fake_cb_bridge_bus_number = bus->number;
  301. return fake_cb_bridge_read(where, size, val);
  302. }
  303. /* a configuration cycle for the device behind the cardbus
  304. * bridge is actually done as a type 0 cycle on the primary
  305. * bus. This means that only one device can be on the cardbus
  306. * bus */
  307. if (fake_cb_bridge_regs.bus_assigned &&
  308. bus->number == fake_cb_bridge_regs.cardbus_busn &&
  309. PCI_SLOT(devfn) == 0)
  310. return bcm63xx_do_cfg_read(0, 0,
  311. PCI_DEVFN(CARDBUS_PCI_IDSEL, 0),
  312. where, size, val);
  313. return PCIBIOS_DEVICE_NOT_FOUND;
  314. }
  315. static int bcm63xx_cb_write(struct pci_bus *bus, unsigned int devfn,
  316. int where, int size, u32 val)
  317. {
  318. if (!bus->parent && PCI_SLOT(devfn) == FAKE_CB_BRIDGE_SLOT) {
  319. fake_cb_bridge_bus_number = bus->number;
  320. return fake_cb_bridge_write(where, size, val);
  321. }
  322. if (fake_cb_bridge_regs.bus_assigned &&
  323. bus->number == fake_cb_bridge_regs.cardbus_busn &&
  324. PCI_SLOT(devfn) == 0)
  325. return bcm63xx_do_cfg_write(0, 0,
  326. PCI_DEVFN(CARDBUS_PCI_IDSEL, 0),
  327. where, size, val);
  328. return PCIBIOS_DEVICE_NOT_FOUND;
  329. }
  330. struct pci_ops bcm63xx_cb_ops = {
  331. .read = bcm63xx_cb_read,
  332. .write = bcm63xx_cb_write,
  333. };
  334. /*
  335. * only one IO window, so it cannot be shared by PCI and cardbus, use
  336. * fixup to choose and detect unhandled configuration
  337. */
  338. static void bcm63xx_fixup(struct pci_dev *dev)
  339. {
  340. static int io_window = -1;
  341. int i, found, new_io_window;
  342. u32 val;
  343. /* look for any io resource */
  344. found = 0;
  345. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  346. if (pci_resource_flags(dev, i) & IORESOURCE_IO) {
  347. found = 1;
  348. break;
  349. }
  350. }
  351. if (!found)
  352. return;
  353. /* skip our fake bus with only cardbus bridge on it */
  354. if (dev->bus->number == fake_cb_bridge_bus_number)
  355. return;
  356. /* find on which bus the device is */
  357. if (fake_cb_bridge_regs.bus_assigned &&
  358. dev->bus->number == fake_cb_bridge_regs.cardbus_busn &&
  359. PCI_SLOT(dev->devfn) == 0)
  360. new_io_window = 1;
  361. else
  362. new_io_window = 0;
  363. if (new_io_window == io_window)
  364. return;
  365. if (io_window != -1) {
  366. printk(KERN_ERR "bcm63xx: both PCI and cardbus devices "
  367. "need IO, which hardware cannot do\n");
  368. return;
  369. }
  370. printk(KERN_INFO "bcm63xx: PCI IO window assigned to %s\n",
  371. (new_io_window == 0) ? "PCI" : "cardbus");
  372. val = bcm_mpi_readl(MPI_L2PIOREMAP_REG);
  373. if (io_window)
  374. val |= MPI_L2PREMAP_IS_CARDBUS_MASK;
  375. else
  376. val &= ~MPI_L2PREMAP_IS_CARDBUS_MASK;
  377. bcm_mpi_writel(val, MPI_L2PIOREMAP_REG);
  378. io_window = new_io_window;
  379. }
  380. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, bcm63xx_fixup);
  381. #endif
  382. static int bcm63xx_pcie_can_access(struct pci_bus *bus, int devfn)
  383. {
  384. switch (bus->number) {
  385. case PCIE_BUS_BRIDGE:
  386. return PCI_SLOT(devfn) == 0;
  387. case PCIE_BUS_DEVICE:
  388. if (PCI_SLOT(devfn) == 0)
  389. return bcm_pcie_readl(PCIE_DLSTATUS_REG)
  390. & DLSTATUS_PHYLINKUP;
  391. default:
  392. return false;
  393. }
  394. }
  395. static int bcm63xx_pcie_read(struct pci_bus *bus, unsigned int devfn,
  396. int where, int size, u32 *val)
  397. {
  398. u32 data;
  399. u32 reg = where & ~3;
  400. if (!bcm63xx_pcie_can_access(bus, devfn))
  401. return PCIBIOS_DEVICE_NOT_FOUND;
  402. if (bus->number == PCIE_BUS_DEVICE)
  403. reg += PCIE_DEVICE_OFFSET;
  404. data = bcm_pcie_readl(reg);
  405. *val = postprocess_read(data, where, size);
  406. return PCIBIOS_SUCCESSFUL;
  407. }
  408. static int bcm63xx_pcie_write(struct pci_bus *bus, unsigned int devfn,
  409. int where, int size, u32 val)
  410. {
  411. u32 data;
  412. u32 reg = where & ~3;
  413. if (!bcm63xx_pcie_can_access(bus, devfn))
  414. return PCIBIOS_DEVICE_NOT_FOUND;
  415. if (bus->number == PCIE_BUS_DEVICE)
  416. reg += PCIE_DEVICE_OFFSET;
  417. data = bcm_pcie_readl(reg);
  418. data = preprocess_write(data, val, where, size);
  419. bcm_pcie_writel(data, reg);
  420. return PCIBIOS_SUCCESSFUL;
  421. }
  422. struct pci_ops bcm63xx_pcie_ops = {
  423. .read = bcm63xx_pcie_read,
  424. .write = bcm63xx_pcie_write
  425. };