mpc10x_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * arch/ppc/syslib/mpc10x_common.c
  3. *
  4. * Common routines for the Motorola SPS MPC106, MPC107 and MPC8240 Host bridge,
  5. * Mem ctlr, EPIC, etc.
  6. *
  7. * Author: Mark A. Greer
  8. * mgreer@mvista.com
  9. *
  10. * 2001 (c) MontaVista, Software, Inc. This file is licensed under
  11. * the terms of the GNU General Public License version 2. This program
  12. * is licensed "as is" without any warranty of any kind, whether express
  13. * or implied.
  14. */
  15. /*
  16. * *** WARNING - A BAT MUST be set to access the PCI config addr/data regs ***
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/pci.h>
  21. #include <linux/slab.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/io.h>
  24. #include <asm/irq.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/machdep.h>
  27. #include <asm/pci-bridge.h>
  28. #include <asm/open_pic.h>
  29. #include <asm/mpc10x.h>
  30. #include <asm/ocp.h>
  31. /* The OCP structure is fixed by code below, before OCP initialises.
  32. paddr depends on where the board places the EUMB.
  33. - fixed in mpc10x_bridge_init().
  34. irq depends on two things:
  35. > does the board use the EPIC at all? (PCORE does not).
  36. > is the EPIC in serial or parallel mode?
  37. - fixed in mpc10x_set_openpic().
  38. */
  39. #ifdef CONFIG_MPC10X_OPENPIC
  40. #ifdef CONFIG_EPIC_SERIAL_MODE
  41. #define EPIC_IRQ_BASE (epic_serial_mode ? 16 : 5)
  42. #else
  43. #define EPIC_IRQ_BASE 5
  44. #endif
  45. #define MPC10X_I2C_IRQ (EPIC_IRQ_BASE + NUM_8259_INTERRUPTS)
  46. #define MPC10X_DMA0_IRQ (EPIC_IRQ_BASE + 1 + NUM_8259_INTERRUPTS)
  47. #define MPC10X_DMA1_IRQ (EPIC_IRQ_BASE + 2 + NUM_8259_INTERRUPTS)
  48. #else
  49. #define MPC10X_I2C_IRQ OCP_IRQ_NA
  50. #define MPC10X_DMA0_IRQ OCP_IRQ_NA
  51. #define MPC10X_DMA1_IRQ OCP_IRQ_NA
  52. #endif
  53. struct ocp_def core_ocp[] = {
  54. { .vendor = OCP_VENDOR_INVALID
  55. }
  56. };
  57. static struct ocp_fs_i2c_data mpc10x_i2c_data = {
  58. .flags = 0
  59. };
  60. static struct ocp_def mpc10x_i2c_ocp = {
  61. .vendor = OCP_VENDOR_MOTOROLA,
  62. .function = OCP_FUNC_IIC,
  63. .index = 0,
  64. .additions = &mpc10x_i2c_data
  65. };
  66. static struct ocp_def mpc10x_dma_ocp[2] = {
  67. { .vendor = OCP_VENDOR_MOTOROLA,
  68. .function = OCP_FUNC_DMA,
  69. .index = 0 },
  70. { .vendor = OCP_VENDOR_MOTOROLA,
  71. .function = OCP_FUNC_DMA,
  72. .index = 1 }
  73. };
  74. /* Set resources to match bridge memory map */
  75. void __init
  76. mpc10x_bridge_set_resources(int map, struct pci_controller *hose)
  77. {
  78. switch (map) {
  79. case MPC10X_MEM_MAP_A:
  80. pci_init_resource(&hose->io_resource,
  81. 0x00000000,
  82. 0x3f7fffff,
  83. IORESOURCE_IO,
  84. "PCI host bridge");
  85. pci_init_resource (&hose->mem_resources[0],
  86. 0xc0000000,
  87. 0xfeffffff,
  88. IORESOURCE_MEM,
  89. "PCI host bridge");
  90. break;
  91. case MPC10X_MEM_MAP_B:
  92. pci_init_resource(&hose->io_resource,
  93. 0x00000000,
  94. 0x00bfffff,
  95. IORESOURCE_IO,
  96. "PCI host bridge");
  97. pci_init_resource (&hose->mem_resources[0],
  98. 0x80000000,
  99. 0xfcffffff,
  100. IORESOURCE_MEM,
  101. "PCI host bridge");
  102. break;
  103. default:
  104. printk("mpc10x_bridge_set_resources: "
  105. "Invalid map specified\n");
  106. if (ppc_md.progress)
  107. ppc_md.progress("mpc10x:exit1", 0x100);
  108. }
  109. }
  110. /*
  111. * Do some initialization and put the EUMB registers at the specified address
  112. * (also map the EPIC registers into virtual space--OpenPIC_Addr will be set).
  113. *
  114. * The EPIC is not on the 106, only the 8240 and 107.
  115. */
  116. int __init
  117. mpc10x_bridge_init(struct pci_controller *hose,
  118. uint current_map,
  119. uint new_map,
  120. uint phys_eumb_base)
  121. {
  122. int host_bridge, picr1, picr1_bit;
  123. ulong pci_config_addr, pci_config_data;
  124. u_char pir, byte;
  125. if (ppc_md.progress) ppc_md.progress("mpc10x:enter", 0x100);
  126. /* Set up for current map so we can get at config regs */
  127. switch (current_map) {
  128. case MPC10X_MEM_MAP_A:
  129. setup_indirect_pci(hose,
  130. MPC10X_MAPA_CNFG_ADDR,
  131. MPC10X_MAPA_CNFG_DATA);
  132. break;
  133. case MPC10X_MEM_MAP_B:
  134. setup_indirect_pci(hose,
  135. MPC10X_MAPB_CNFG_ADDR,
  136. MPC10X_MAPB_CNFG_DATA);
  137. break;
  138. default:
  139. printk("mpc10x_bridge_init: %s\n",
  140. "Invalid current map specified");
  141. if (ppc_md.progress)
  142. ppc_md.progress("mpc10x:exit1", 0x100);
  143. return -1;
  144. }
  145. /* Make sure it's a supported bridge */
  146. early_read_config_dword(hose,
  147. 0,
  148. PCI_DEVFN(0,0),
  149. PCI_VENDOR_ID,
  150. &host_bridge);
  151. switch (host_bridge) {
  152. case MPC10X_BRIDGE_106:
  153. case MPC10X_BRIDGE_8240:
  154. case MPC10X_BRIDGE_107:
  155. case MPC10X_BRIDGE_8245:
  156. break;
  157. default:
  158. if (ppc_md.progress)
  159. ppc_md.progress("mpc10x:exit2", 0x100);
  160. return -1;
  161. }
  162. switch (new_map) {
  163. case MPC10X_MEM_MAP_A:
  164. MPC10X_SETUP_HOSE(hose, A);
  165. pci_config_addr = MPC10X_MAPA_CNFG_ADDR;
  166. pci_config_data = MPC10X_MAPA_CNFG_DATA;
  167. picr1_bit = MPC10X_CFG_PICR1_ADDR_MAP_A;
  168. break;
  169. case MPC10X_MEM_MAP_B:
  170. MPC10X_SETUP_HOSE(hose, B);
  171. pci_config_addr = MPC10X_MAPB_CNFG_ADDR;
  172. pci_config_data = MPC10X_MAPB_CNFG_DATA;
  173. picr1_bit = MPC10X_CFG_PICR1_ADDR_MAP_B;
  174. break;
  175. default:
  176. printk("mpc10x_bridge_init: %s\n",
  177. "Invalid new map specified");
  178. if (ppc_md.progress)
  179. ppc_md.progress("mpc10x:exit3", 0x100);
  180. return -1;
  181. }
  182. /* Make bridge use the 'new_map', if not already usng it */
  183. if (current_map != new_map) {
  184. early_read_config_dword(hose,
  185. 0,
  186. PCI_DEVFN(0,0),
  187. MPC10X_CFG_PICR1_REG,
  188. &picr1);
  189. picr1 = (picr1 & ~MPC10X_CFG_PICR1_ADDR_MAP_MASK) |
  190. picr1_bit;
  191. early_write_config_dword(hose,
  192. 0,
  193. PCI_DEVFN(0,0),
  194. MPC10X_CFG_PICR1_REG,
  195. picr1);
  196. asm volatile("sync");
  197. /* Undo old mappings & map in new cfg data/addr regs */
  198. iounmap((void *)hose->cfg_addr);
  199. iounmap((void *)hose->cfg_data);
  200. setup_indirect_pci(hose,
  201. pci_config_addr,
  202. pci_config_data);
  203. }
  204. /* Setup resources to match map */
  205. mpc10x_bridge_set_resources(new_map, hose);
  206. /*
  207. * Want processor accesses of 0xFDxxxxxx to be mapped
  208. * to PCI memory space at 0x00000000. Do not want
  209. * host bridge to respond to PCI memory accesses of
  210. * 0xFDxxxxxx. Do not want host bridge to respond
  211. * to PCI memory addresses 0xFD000000-0xFDFFFFFF;
  212. * want processor accesses from 0x000A0000-0x000BFFFF
  213. * to be forwarded to system memory.
  214. *
  215. * Only valid if not in agent mode and using MAP B.
  216. */
  217. if (new_map == MPC10X_MEM_MAP_B) {
  218. early_read_config_byte(hose,
  219. 0,
  220. PCI_DEVFN(0,0),
  221. MPC10X_CFG_MAPB_OPTIONS_REG,
  222. &byte);
  223. byte &= ~(MPC10X_CFG_MAPB_OPTIONS_PFAE |
  224. MPC10X_CFG_MAPB_OPTIONS_PCICH |
  225. MPC10X_CFG_MAPB_OPTIONS_PROCCH);
  226. if (host_bridge != MPC10X_BRIDGE_106) {
  227. byte |= MPC10X_CFG_MAPB_OPTIONS_CFAE;
  228. }
  229. early_write_config_byte(hose,
  230. 0,
  231. PCI_DEVFN(0,0),
  232. MPC10X_CFG_MAPB_OPTIONS_REG,
  233. byte);
  234. }
  235. if (host_bridge != MPC10X_BRIDGE_106) {
  236. early_read_config_byte(hose,
  237. 0,
  238. PCI_DEVFN(0,0),
  239. MPC10X_CFG_PIR_REG,
  240. &pir);
  241. if (pir != MPC10X_CFG_PIR_HOST_BRIDGE) {
  242. printk("Host bridge in Agent mode\n");
  243. /* Read or Set LMBAR & PCSRBAR? */
  244. }
  245. /* Set base addr of the 8240/107 EUMB. */
  246. early_write_config_dword(hose,
  247. 0,
  248. PCI_DEVFN(0,0),
  249. MPC10X_CFG_EUMBBAR,
  250. phys_eumb_base);
  251. #ifdef CONFIG_MPC10X_OPENPIC
  252. /* Map EPIC register part of EUMB into vitual memory - PCORE
  253. uses an i8259 instead of EPIC. */
  254. OpenPIC_Addr =
  255. ioremap(phys_eumb_base + MPC10X_EUMB_EPIC_OFFSET,
  256. MPC10X_EUMB_EPIC_SIZE);
  257. #endif
  258. mpc10x_i2c_ocp.paddr = phys_eumb_base + MPC10X_EUMB_I2C_OFFSET;
  259. mpc10x_i2c_ocp.irq = MPC10X_I2C_IRQ;
  260. ocp_add_one_device(&mpc10x_i2c_ocp);
  261. mpc10x_dma_ocp[0].paddr = phys_eumb_base +
  262. MPC10X_EUMB_DMA_OFFSET + 0x100;
  263. mpc10x_dma_ocp[0].irq = MPC10X_DMA0_IRQ;
  264. ocp_add_one_device(&mpc10x_dma_ocp[0]);
  265. mpc10x_dma_ocp[1].paddr = phys_eumb_base +
  266. MPC10X_EUMB_DMA_OFFSET + 0x200;
  267. mpc10x_dma_ocp[1].irq = MPC10X_DMA1_IRQ;
  268. ocp_add_one_device(&mpc10x_dma_ocp[1]);
  269. }
  270. #ifdef CONFIG_MPC10X_STORE_GATHERING
  271. mpc10x_enable_store_gathering(hose);
  272. #else
  273. mpc10x_disable_store_gathering(hose);
  274. #endif
  275. /*
  276. * 8240 erratum 26, 8241/8245 erratum 29, 107 erratum 23: speculative
  277. * PCI reads may return stale data so turn off.
  278. */
  279. if ((host_bridge == MPC10X_BRIDGE_8240)
  280. || (host_bridge == MPC10X_BRIDGE_8245)
  281. || (host_bridge == MPC10X_BRIDGE_107)) {
  282. early_read_config_dword(hose, 0, PCI_DEVFN(0,0),
  283. MPC10X_CFG_PICR1_REG, &picr1);
  284. picr1 &= ~MPC10X_CFG_PICR1_SPEC_PCI_RD;
  285. early_write_config_dword(hose, 0, PCI_DEVFN(0,0),
  286. MPC10X_CFG_PICR1_REG, picr1);
  287. }
  288. /*
  289. * 8241/8245 erratum 28: PCI reads from local memory may return
  290. * stale data. Workaround by setting PICR2[0] to disable copyback
  291. * optimization. Oddly, the latest available user manual for the
  292. * 8245 (Rev 2., dated 10/2003) says PICR2[0] is reserverd.
  293. */
  294. if (host_bridge == MPC10X_BRIDGE_8245) {
  295. ulong picr2;
  296. early_read_config_dword(hose, 0, PCI_DEVFN(0,0),
  297. MPC10X_CFG_PICR2_REG, &picr2);
  298. picr2 |= MPC10X_CFG_PICR2_COPYBACK_OPT;
  299. early_write_config_dword(hose, 0, PCI_DEVFN(0,0),
  300. MPC10X_CFG_PICR2_REG, picr2);
  301. }
  302. if (ppc_md.progress) ppc_md.progress("mpc10x:exit", 0x100);
  303. return 0;
  304. }
  305. /*
  306. * Need to make our own PCI config space access macros because
  307. * mpc10x_get_mem_size() is called before the data structures are set up for
  308. * the 'early_xxx' and 'indirect_xxx' routines to work.
  309. * Assumes bus 0.
  310. */
  311. #define MPC10X_CFG_read(val, addr, type, op) *val = op((type)(addr))
  312. #define MPC10X_CFG_write(val, addr, type, op) op((type *)(addr), (val))
  313. #define MPC10X_PCI_OP(rw, size, type, op, mask) \
  314. static void \
  315. mpc10x_##rw##_config_##size(uint *cfg_addr, uint *cfg_data, int devfn, int offset, type val) \
  316. { \
  317. out_be32(cfg_addr, \
  318. ((offset & 0xfc) << 24) | (devfn << 16) \
  319. | (0 << 8) | 0x80); \
  320. MPC10X_CFG_##rw(val, cfg_data + (offset & mask), type, op); \
  321. return; \
  322. }
  323. MPC10X_PCI_OP(read, byte, u8 *, in_8, 3)
  324. MPC10X_PCI_OP(read, dword, u32 *, in_le32, 0)
  325. #if 0 /* Not used */
  326. MPC10X_PCI_OP(write, byte, u8, out_8, 3)
  327. MPC10X_PCI_OP(read, word, u16 *, in_le16, 2)
  328. MPC10X_PCI_OP(write, word, u16, out_le16, 2)
  329. MPC10X_PCI_OP(write, dword, u32, out_le32, 0)
  330. #endif
  331. /*
  332. * Read the memory controller registers to determine the amount of memory in
  333. * the system. This assumes that the firmware has correctly set up the memory
  334. * controller registers.
  335. */
  336. unsigned long __init
  337. mpc10x_get_mem_size(uint mem_map)
  338. {
  339. uint *config_addr, *config_data, val;
  340. ulong start, end, total, offset;
  341. int i;
  342. u_char bank_enables;
  343. switch (mem_map) {
  344. case MPC10X_MEM_MAP_A:
  345. config_addr = (uint *)MPC10X_MAPA_CNFG_ADDR;
  346. config_data = (uint *)MPC10X_MAPA_CNFG_DATA;
  347. break;
  348. case MPC10X_MEM_MAP_B:
  349. config_addr = (uint *)MPC10X_MAPB_CNFG_ADDR;
  350. config_data = (uint *)MPC10X_MAPB_CNFG_DATA;
  351. break;
  352. default:
  353. return 0;
  354. }
  355. mpc10x_read_config_byte(config_addr,
  356. config_data,
  357. PCI_DEVFN(0,0),
  358. MPC10X_MCTLR_MEM_BANK_ENABLES,
  359. &bank_enables);
  360. total = 0;
  361. for (i=0; i<8; i++) {
  362. if (bank_enables & (1 << i)) {
  363. offset = MPC10X_MCTLR_MEM_START_1 + ((i > 3) ? 4 : 0);
  364. mpc10x_read_config_dword(config_addr,
  365. config_data,
  366. PCI_DEVFN(0,0),
  367. offset,
  368. &val);
  369. start = (val >> ((i & 3) << 3)) & 0xff;
  370. offset = MPC10X_MCTLR_EXT_MEM_START_1 + ((i>3) ? 4 : 0);
  371. mpc10x_read_config_dword(config_addr,
  372. config_data,
  373. PCI_DEVFN(0,0),
  374. offset,
  375. &val);
  376. val = (val >> ((i & 3) << 3)) & 0x03;
  377. start = (val << 28) | (start << 20);
  378. offset = MPC10X_MCTLR_MEM_END_1 + ((i > 3) ? 4 : 0);
  379. mpc10x_read_config_dword(config_addr,
  380. config_data,
  381. PCI_DEVFN(0,0),
  382. offset,
  383. &val);
  384. end = (val >> ((i & 3) << 3)) & 0xff;
  385. offset = MPC10X_MCTLR_EXT_MEM_END_1 + ((i > 3) ? 4 : 0);
  386. mpc10x_read_config_dword(config_addr,
  387. config_data,
  388. PCI_DEVFN(0,0),
  389. offset,
  390. &val);
  391. val = (val >> ((i & 3) << 3)) & 0x03;
  392. end = (val << 28) | (end << 20) | 0xfffff;
  393. total += (end - start + 1);
  394. }
  395. }
  396. return total;
  397. }
  398. int __init
  399. mpc10x_enable_store_gathering(struct pci_controller *hose)
  400. {
  401. uint picr1;
  402. early_read_config_dword(hose,
  403. 0,
  404. PCI_DEVFN(0,0),
  405. MPC10X_CFG_PICR1_REG,
  406. &picr1);
  407. picr1 |= MPC10X_CFG_PICR1_ST_GATH_EN;
  408. early_write_config_dword(hose,
  409. 0,
  410. PCI_DEVFN(0,0),
  411. MPC10X_CFG_PICR1_REG,
  412. picr1);
  413. return 0;
  414. }
  415. int __init
  416. mpc10x_disable_store_gathering(struct pci_controller *hose)
  417. {
  418. uint picr1;
  419. early_read_config_dword(hose,
  420. 0,
  421. PCI_DEVFN(0,0),
  422. MPC10X_CFG_PICR1_REG,
  423. &picr1);
  424. picr1 &= ~MPC10X_CFG_PICR1_ST_GATH_EN;
  425. early_write_config_dword(hose,
  426. 0,
  427. PCI_DEVFN(0,0),
  428. MPC10X_CFG_PICR1_REG,
  429. picr1);
  430. return 0;
  431. }
  432. #ifdef CONFIG_MPC10X_OPENPIC
  433. void __init mpc10x_set_openpic(void)
  434. {
  435. /* Map external IRQs */
  436. openpic_set_sources(0, EPIC_IRQ_BASE, OpenPIC_Addr + 0x10200);
  437. /* Skip reserved space and map i2c and DMA Ch[01] */
  438. openpic_set_sources(EPIC_IRQ_BASE, 3, OpenPIC_Addr + 0x11020);
  439. /* Skip reserved space and map Message Unit Interrupt (I2O) */
  440. openpic_set_sources(EPIC_IRQ_BASE + 3, 1, OpenPIC_Addr + 0x110C0);
  441. openpic_init(NUM_8259_INTERRUPTS);
  442. }
  443. #endif