ops-bcm63xx.c 11 KB

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