pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Support for PCI on Celleb platform.
  3. *
  4. * (C) Copyright 2006-2007 TOSHIBA CORPORATION
  5. *
  6. * This code is based on arch/powerpc/kernel/rtas_pci.c:
  7. * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
  8. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #undef DEBUG
  25. #include <linux/kernel.h>
  26. #include <linux/threads.h>
  27. #include <linux/pci.h>
  28. #include <linux/string.h>
  29. #include <linux/init.h>
  30. #include <linux/bootmem.h>
  31. #include <linux/pci_regs.h>
  32. #include <asm/io.h>
  33. #include <asm/irq.h>
  34. #include <asm/prom.h>
  35. #include <asm/machdep.h>
  36. #include <asm/pci-bridge.h>
  37. #include <asm/ppc-pci.h>
  38. #include "pci.h"
  39. #include "interrupt.h"
  40. #define MAX_PCI_DEVICES 32
  41. #define MAX_PCI_FUNCTIONS 8
  42. #define MAX_PCI_BASE_ADDRS 3 /* use 64 bit address */
  43. /* definition for fake pci configuration area for GbE, .... ,and etc. */
  44. struct celleb_pci_resource {
  45. struct resource r[MAX_PCI_BASE_ADDRS];
  46. };
  47. struct celleb_pci_private {
  48. unsigned char *fake_config[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
  49. struct celleb_pci_resource *res[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
  50. };
  51. static inline u8 celleb_fake_config_readb(void *addr)
  52. {
  53. u8 *p = addr;
  54. return *p;
  55. }
  56. static inline u16 celleb_fake_config_readw(void *addr)
  57. {
  58. __le16 *p = addr;
  59. return le16_to_cpu(*p);
  60. }
  61. static inline u32 celleb_fake_config_readl(void *addr)
  62. {
  63. __le32 *p = addr;
  64. return le32_to_cpu(*p);
  65. }
  66. static inline void celleb_fake_config_writeb(u32 val, void *addr)
  67. {
  68. u8 *p = addr;
  69. *p = val;
  70. }
  71. static inline void celleb_fake_config_writew(u32 val, void *addr)
  72. {
  73. __le16 val16;
  74. __le16 *p = addr;
  75. val16 = cpu_to_le16(val);
  76. *p = val16;
  77. }
  78. static inline void celleb_fake_config_writel(u32 val, void *addr)
  79. {
  80. __le32 val32;
  81. __le32 *p = addr;
  82. val32 = cpu_to_le32(val);
  83. *p = val32;
  84. }
  85. static unsigned char *get_fake_config_start(struct pci_controller *hose,
  86. int devno, int fn)
  87. {
  88. struct celleb_pci_private *private = hose->private_data;
  89. if (private == NULL)
  90. return NULL;
  91. return private->fake_config[devno][fn];
  92. }
  93. static struct celleb_pci_resource *get_resource_start(
  94. struct pci_controller *hose,
  95. int devno, int fn)
  96. {
  97. struct celleb_pci_private *private = hose->private_data;
  98. if (private == NULL)
  99. return NULL;
  100. return private->res[devno][fn];
  101. }
  102. static void celleb_config_read_fake(unsigned char *config, int where,
  103. int size, u32 *val)
  104. {
  105. char *p = config + where;
  106. switch (size) {
  107. case 1:
  108. *val = celleb_fake_config_readb(p);
  109. break;
  110. case 2:
  111. *val = celleb_fake_config_readw(p);
  112. break;
  113. case 4:
  114. *val = celleb_fake_config_readl(p);
  115. break;
  116. }
  117. return;
  118. }
  119. static void celleb_config_write_fake(unsigned char *config, int where,
  120. int size, u32 val)
  121. {
  122. char *p = config + where;
  123. switch (size) {
  124. case 1:
  125. celleb_fake_config_writeb(val, p);
  126. break;
  127. case 2:
  128. celleb_fake_config_writew(val, p);
  129. break;
  130. case 4:
  131. celleb_fake_config_writel(val, p);
  132. break;
  133. }
  134. return;
  135. }
  136. static int celleb_fake_pci_read_config(struct pci_bus *bus,
  137. unsigned int devfn, int where, int size, u32 *val)
  138. {
  139. char *config;
  140. struct device_node *node;
  141. struct pci_controller *hose;
  142. unsigned int devno = devfn >> 3;
  143. unsigned int fn = devfn & 0x7;
  144. /* allignment check */
  145. BUG_ON(where % size);
  146. pr_debug(" fake read: bus=0x%x, ", bus->number);
  147. node = (struct device_node *)bus->sysdata;
  148. hose = pci_find_hose_for_OF_device(node);
  149. config = get_fake_config_start(hose, devno, fn);
  150. pr_debug("devno=0x%x, where=0x%x, size=0x%x, ", devno, where, size);
  151. if (!config) {
  152. pr_debug("failed\n");
  153. return PCIBIOS_DEVICE_NOT_FOUND;
  154. }
  155. celleb_config_read_fake(config, where, size, val);
  156. pr_debug("val=0x%x\n", *val);
  157. return PCIBIOS_SUCCESSFUL;
  158. }
  159. static int celleb_fake_pci_write_config(struct pci_bus *bus,
  160. unsigned int devfn, int where, int size, u32 val)
  161. {
  162. char *config;
  163. struct device_node *node;
  164. struct pci_controller *hose;
  165. struct celleb_pci_resource *res;
  166. unsigned int devno = devfn >> 3;
  167. unsigned int fn = devfn & 0x7;
  168. /* allignment check */
  169. BUG_ON(where % size);
  170. node = (struct device_node *)bus->sysdata;
  171. hose = pci_find_hose_for_OF_device(node);
  172. config = get_fake_config_start(hose, devno, fn);
  173. if (!config)
  174. return PCIBIOS_DEVICE_NOT_FOUND;
  175. if (val == ~0) {
  176. int i = (where - PCI_BASE_ADDRESS_0) >> 3;
  177. switch (where) {
  178. case PCI_BASE_ADDRESS_0:
  179. case PCI_BASE_ADDRESS_2:
  180. if (size != 4)
  181. return PCIBIOS_DEVICE_NOT_FOUND;
  182. res = get_resource_start(hose, devno, fn);
  183. if (!res)
  184. return PCIBIOS_DEVICE_NOT_FOUND;
  185. celleb_config_write_fake(config, where, size,
  186. (res->r[i].end - res->r[i].start));
  187. return PCIBIOS_SUCCESSFUL;
  188. case PCI_BASE_ADDRESS_1:
  189. case PCI_BASE_ADDRESS_3:
  190. case PCI_BASE_ADDRESS_4:
  191. case PCI_BASE_ADDRESS_5:
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. celleb_config_write_fake(config, where, size, val);
  198. pr_debug(" fake write: where=%x, size=%d, val=%x\n",
  199. where, size, val);
  200. return PCIBIOS_SUCCESSFUL;
  201. }
  202. static struct pci_ops celleb_fake_pci_ops = {
  203. celleb_fake_pci_read_config,
  204. celleb_fake_pci_write_config
  205. };
  206. static inline void celleb_setup_pci_base_addrs(struct pci_controller *hose,
  207. unsigned int devno, unsigned int fn,
  208. unsigned int num_base_addr)
  209. {
  210. u32 val;
  211. unsigned char *config;
  212. struct celleb_pci_resource *res;
  213. config = get_fake_config_start(hose, devno, fn);
  214. res = get_resource_start(hose, devno, fn);
  215. if (!config || !res)
  216. return;
  217. switch (num_base_addr) {
  218. case 3:
  219. val = (res->r[2].start & 0xfffffff0)
  220. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  221. celleb_config_write_fake(config, PCI_BASE_ADDRESS_4, 4, val);
  222. val = res->r[2].start >> 32;
  223. celleb_config_write_fake(config, PCI_BASE_ADDRESS_5, 4, val);
  224. /* FALLTHROUGH */
  225. case 2:
  226. val = (res->r[1].start & 0xfffffff0)
  227. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  228. celleb_config_write_fake(config, PCI_BASE_ADDRESS_2, 4, val);
  229. val = res->r[1].start >> 32;
  230. celleb_config_write_fake(config, PCI_BASE_ADDRESS_3, 4, val);
  231. /* FALLTHROUGH */
  232. case 1:
  233. val = (res->r[0].start & 0xfffffff0)
  234. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  235. celleb_config_write_fake(config, PCI_BASE_ADDRESS_0, 4, val);
  236. val = res->r[0].start >> 32;
  237. celleb_config_write_fake(config, PCI_BASE_ADDRESS_1, 4, val);
  238. break;
  239. }
  240. val = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  241. celleb_config_write_fake(config, PCI_COMMAND, 2, val);
  242. }
  243. static int __devinit celleb_setup_fake_pci_device(struct device_node *node,
  244. struct pci_controller *hose)
  245. {
  246. unsigned int rlen;
  247. int num_base_addr = 0;
  248. u32 val;
  249. const u32 *wi0, *wi1, *wi2, *wi3, *wi4;
  250. unsigned int devno, fn;
  251. struct celleb_pci_private *private = hose->private_data;
  252. unsigned char **config = NULL;
  253. struct celleb_pci_resource **res = NULL;
  254. const char *name;
  255. const unsigned long *li;
  256. int size, result;
  257. if (private == NULL) {
  258. printk(KERN_ERR "PCI: "
  259. "memory space for pci controller is not assigned\n");
  260. goto error;
  261. }
  262. name = get_property(node, "model", &rlen);
  263. if (!name) {
  264. printk(KERN_ERR "PCI: model property not found.\n");
  265. goto error;
  266. }
  267. wi4 = get_property(node, "reg", &rlen);
  268. if (wi4 == NULL)
  269. goto error;
  270. devno = ((wi4[0] >> 8) & 0xff) >> 3;
  271. fn = (wi4[0] >> 8) & 0x7;
  272. pr_debug("PCI: celleb_setup_fake_pci() %s devno=%x fn=%x\n", name,
  273. devno, fn);
  274. size = 256;
  275. config = &private->fake_config[devno][fn];
  276. if (mem_init_done)
  277. *config = kzalloc(size, GFP_KERNEL);
  278. else
  279. *config = alloc_bootmem(size);
  280. if (*config == NULL) {
  281. printk(KERN_ERR "PCI: "
  282. "not enough memory for fake configuration space\n");
  283. goto error;
  284. }
  285. pr_debug("PCI: fake config area assigned 0x%016lx\n",
  286. (unsigned long)*config);
  287. size = sizeof(struct celleb_pci_resource);
  288. res = &private->res[devno][fn];
  289. if (mem_init_done)
  290. *res = kzalloc(size, GFP_KERNEL);
  291. else
  292. *res = alloc_bootmem(size);
  293. if (*res == NULL) {
  294. printk(KERN_ERR
  295. "PCI: not enough memory for resource data space\n");
  296. goto error;
  297. }
  298. pr_debug("PCI: res assigned 0x%016lx\n", (unsigned long)*res);
  299. wi0 = get_property(node, "device-id", NULL);
  300. wi1 = get_property(node, "vendor-id", NULL);
  301. wi2 = get_property(node, "class-code", NULL);
  302. wi3 = get_property(node, "revision-id", NULL);
  303. celleb_config_write_fake(*config, PCI_DEVICE_ID, 2, wi0[0] & 0xffff);
  304. celleb_config_write_fake(*config, PCI_VENDOR_ID, 2, wi1[0] & 0xffff);
  305. pr_debug("class-code = 0x%08x\n", wi2[0]);
  306. celleb_config_write_fake(*config, PCI_CLASS_PROG, 1, wi2[0] & 0xff);
  307. celleb_config_write_fake(*config, PCI_CLASS_DEVICE, 2,
  308. (wi2[0] >> 8) & 0xffff);
  309. celleb_config_write_fake(*config, PCI_REVISION_ID, 1, wi3[0]);
  310. while (num_base_addr < MAX_PCI_BASE_ADDRS) {
  311. result = of_address_to_resource(node,
  312. num_base_addr, &(*res)->r[num_base_addr]);
  313. if (result)
  314. break;
  315. num_base_addr++;
  316. }
  317. celleb_setup_pci_base_addrs(hose, devno, fn, num_base_addr);
  318. li = get_property(node, "interrupts", &rlen);
  319. val = li[0];
  320. celleb_config_write_fake(*config, PCI_INTERRUPT_PIN, 1, 1);
  321. celleb_config_write_fake(*config, PCI_INTERRUPT_LINE, 1, val);
  322. #ifdef DEBUG
  323. pr_debug("PCI: %s irq=%ld\n", name, li[0]);
  324. for (i = 0; i < 6; i++) {
  325. celleb_config_read_fake(*config,
  326. PCI_BASE_ADDRESS_0 + 0x4 * i, 4,
  327. &val);
  328. pr_debug("PCI: %s fn=%d base_address_%d=0x%x\n",
  329. name, fn, i, val);
  330. }
  331. #endif
  332. celleb_config_write_fake(*config, PCI_HEADER_TYPE, 1,
  333. PCI_HEADER_TYPE_NORMAL);
  334. return 0;
  335. error:
  336. if (mem_init_done) {
  337. if (config && *config)
  338. kfree(*config);
  339. if (res && *res)
  340. kfree(*res);
  341. } else {
  342. if (config && *config) {
  343. size = 256;
  344. free_bootmem((unsigned long)(*config), size);
  345. }
  346. if (res && *res) {
  347. size = sizeof(struct celleb_pci_resource);
  348. free_bootmem((unsigned long)(*res), size);
  349. }
  350. }
  351. return 1;
  352. }
  353. static int __devinit phb_set_bus_ranges(struct device_node *dev,
  354. struct pci_controller *phb)
  355. {
  356. const int *bus_range;
  357. unsigned int len;
  358. bus_range = get_property(dev, "bus-range", &len);
  359. if (bus_range == NULL || len < 2 * sizeof(int))
  360. return 1;
  361. phb->first_busno = bus_range[0];
  362. phb->last_busno = bus_range[1];
  363. return 0;
  364. }
  365. static void __devinit celleb_alloc_private_mem(struct pci_controller *hose)
  366. {
  367. if (mem_init_done)
  368. hose->private_data =
  369. kzalloc(sizeof(struct celleb_pci_private), GFP_KERNEL);
  370. else
  371. hose->private_data =
  372. alloc_bootmem(sizeof(struct celleb_pci_private));
  373. }
  374. int __devinit celleb_setup_phb(struct pci_controller *phb)
  375. {
  376. const char *name;
  377. struct device_node *dev = phb->arch_data;
  378. struct device_node *node;
  379. unsigned int rlen;
  380. name = get_property(dev, "name", &rlen);
  381. if (!name)
  382. return 1;
  383. pr_debug("PCI: celleb_setup_phb() %s\n", name);
  384. phb_set_bus_ranges(dev, phb);
  385. if (strcmp(name, "epci") == 0) {
  386. phb->ops = &celleb_epci_ops;
  387. return celleb_setup_epci(dev, phb);
  388. } else if (strcmp(name, "pci-pseudo") == 0) {
  389. phb->ops = &celleb_fake_pci_ops;
  390. celleb_alloc_private_mem(phb);
  391. for (node = of_get_next_child(dev, NULL);
  392. node != NULL; node = of_get_next_child(dev, node))
  393. celleb_setup_fake_pci_device(node, phb);
  394. } else
  395. return 1;
  396. return 0;
  397. }
  398. int celleb_pci_probe_mode(struct pci_bus *bus)
  399. {
  400. return PCI_PROBE_DEVTREE;
  401. }