pci-sh7751.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:15k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * Low-Level PCI Support for the SH7751
  3.  *
  4.  *  Dustin McIntire (dustin@sensoria.com)
  5.  * Derived from arch/i386/kernel/pci-*.c which bore the message:
  6.  * (c) 1999--2000 Martin Mares <mj@ucw.cz>
  7.  *
  8.  *  May be copied or modified under the terms of the GNU General Public
  9.  *  License.  See linux/COPYING for more information.
  10.  *
  11. */
  12. #include <linux/config.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/pci.h>
  17. #include <linux/sched.h>
  18. #include <linux/ioport.h>
  19. #include <linux/errno.h>
  20. #include <linux/irq.h>
  21. #include <asm/machvec.h>
  22. #include <asm/io.h>
  23. #include <asm/pci-sh7751.h>
  24. struct pci_ops *pci_check_direct(void);
  25. void pcibios_resource_survey(void);
  26. static u8 pcibios_swizzle(struct pci_dev *dev, u8 *pin);
  27. static int pcibios_lookup_irq(struct pci_dev *dev, u8 slot, u8 pin);
  28. unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1;
  29. int pcibios_last_bus = -1;
  30. struct pci_bus *pci_root_bus;
  31. struct pci_ops *pci_root_ops;
  32. /*
  33.  * Direct access to PCI hardware...
  34.  */
  35. #ifdef CONFIG_PCI_DIRECT
  36. #define CONFIG_CMD(dev, where) (0x80000000 | (dev->bus->number << 16) | (dev->devfn << 8) | (where & ~3))
  37. #define PCI_REG(reg) (SH7751_PCIREG_BASE+reg)
  38. /*
  39.  * Functions for accessing PCI configuration space with type 1 accesses
  40.  */
  41. static int pci_conf1_read_config_byte(struct pci_dev *dev, int where, u8 *value)
  42. {
  43. u32 word;
  44. unsigned long flags;
  45.     /* PCIPDR may only be accessed as 32 bit words, 
  46.      * so we must do byte alignment by hand 
  47.      */
  48. save_and_cli(flags);
  49. outl(CONFIG_CMD(dev,where), PCI_REG(SH7751_PCIPAR));
  50. word = inl(PCI_REG(SH7751_PCIPDR));
  51. restore_flags(flags);
  52. switch (where & 0x3) {
  53.     case 3:
  54.     *value = (u8)(word >> 24);
  55. break;
  56. case 2:
  57.     *value = (u8)(word >> 16);
  58. break;
  59. case 1:
  60.     *value = (u8)(word >> 8);
  61. break;
  62. default:
  63.     *value = (u8)word;
  64. break;
  65.     }
  66. PCIDBG(4,"pci_conf1_read_config_byte@0x%08x=0x%xn",
  67.      CONFIG_CMD(dev,where),*value);
  68. return PCIBIOS_SUCCESSFUL;
  69. }
  70. static int pci_conf1_read_config_word(struct pci_dev *dev, int where, u16 *value)
  71. {
  72. u32 word;
  73. unsigned long flags;
  74.     /* PCIPDR may only be accessed as 32 bit words, 
  75.      * so we must do word alignment by hand 
  76.      */
  77. save_and_cli(flags);
  78. outl(CONFIG_CMD(dev,where), PCI_REG(SH7751_PCIPAR));
  79. word = inl(PCI_REG(SH7751_PCIPDR));
  80. restore_flags(flags);
  81. switch (where & 0x3) {
  82.     case 3:
  83.     // This should never happen...
  84. printk(KERN_ERR "PCI BIOS: read_config_word: Illegal u16 alignment");
  85.         return PCIBIOS_BAD_REGISTER_NUMBER;
  86. case 2:
  87.     *value = (u16)(word >> 16);
  88. break;
  89. case 1:
  90.     *value = (u16)(word >> 8);
  91. break;
  92. default:
  93.     *value = (u16)word;
  94. break;
  95.     }
  96. PCIDBG(4,"pci_conf1_read_config_word@0x%08x=0x%xn",
  97.      CONFIG_CMD(dev,where),*value);
  98. return PCIBIOS_SUCCESSFUL;
  99. }
  100. static int pci_conf1_read_config_dword(struct pci_dev *dev, int where, u32 *value)
  101. {
  102. unsigned long flags;
  103. save_and_cli(flags);
  104. outl(CONFIG_CMD(dev,where), PCI_REG(SH7751_PCIPAR));
  105. *value = inl(PCI_REG(SH7751_PCIPDR));
  106. restore_flags(flags);
  107. PCIDBG(4,"pci_conf1_read_config_dword@0x%08x=0x%xn",
  108.      CONFIG_CMD(dev,where),*value);
  109. return PCIBIOS_SUCCESSFUL;    
  110. }
  111. static int pci_conf1_write_config_byte(struct pci_dev *dev, int where, u8 value)
  112. {
  113. u32 word;
  114. u32 shift = (where & 3) * 8;
  115. u32 mask = ((1 << 8) - 1) << shift;  // create the byte mask
  116. unsigned long flags;
  117.     /* Since SH7751 only does 32bit access we'll have to do a
  118.      * read,mask,write operation
  119.      */ 
  120. save_and_cli(flags);
  121. outl(CONFIG_CMD(dev,where), PCI_REG(SH7751_PCIPAR));
  122. word = inl(PCI_REG(SH7751_PCIPDR)) ;
  123. word &= ~mask;
  124. word |= value << shift;
  125.  
  126. outl(word, PCI_REG(SH7751_PCIPDR));
  127. restore_flags(flags);
  128. PCIDBG(4,"pci_conf1_write_config_byte@0x%08x=0x%xn",
  129.      CONFIG_CMD(dev,where),word);
  130. return PCIBIOS_SUCCESSFUL;
  131. }
  132. static int pci_conf1_write_config_word(struct pci_dev *dev, int where, u16 value)
  133. {
  134. u32 word;
  135. u32 shift = (where & 3) * 8;
  136. u32 mask = ((1 << 16) - 1) << shift;  // create the word mask
  137. unsigned long flags;
  138.     /* Since SH7751 only does 32bit access we'll have to do a
  139.      * read,mask,write operation.  We'll allow an odd byte offset,
  140.  * though it should be illegal.
  141.      */ 
  142. if (shift == 24)
  143.     return PCIBIOS_BAD_REGISTER_NUMBER;
  144. save_and_cli(flags);
  145. outl(CONFIG_CMD(dev,where), PCI_REG(SH7751_PCIPAR));
  146. word = inl(PCI_REG(SH7751_PCIPDR)) ;
  147. word &= ~mask;
  148. word |= value << shift;
  149.  
  150. outl(value, PCI_REG(SH7751_PCIPDR));
  151. restore_flags(flags);
  152. PCIDBG(4,"pci_conf1_write_config_word@0x%08x=0x%xn",
  153.      CONFIG_CMD(dev,where),word);
  154. return PCIBIOS_SUCCESSFUL;
  155. }
  156. static int pci_conf1_write_config_dword(struct pci_dev *dev, int where, u32 value)
  157. {
  158. unsigned long flags;
  159. save_and_cli(flags);
  160. outl(CONFIG_CMD(dev,where), PCI_REG(SH7751_PCIPAR));
  161. outl(value, PCI_REG(SH7751_PCIPDR));
  162. restore_flags(flags);
  163. PCIDBG(4,"pci_conf1_write_config_dword@0x%08x=0x%xn",
  164.      CONFIG_CMD(dev,where),value);
  165. return PCIBIOS_SUCCESSFUL;
  166. }
  167. #undef CONFIG_CMD
  168. static struct pci_ops pci_direct_conf1 = {
  169. pci_conf1_read_config_byte,
  170. pci_conf1_read_config_word,
  171. pci_conf1_read_config_dword,
  172. pci_conf1_write_config_byte,
  173. pci_conf1_write_config_word,
  174. pci_conf1_write_config_dword
  175. };
  176. struct pci_ops * __init pci_check_direct(void)
  177. {
  178. unsigned int tmp, id;
  179. /* check for SH7751 hardware */
  180. id = (SH7751_DEVICE_ID << 16) | SH7751_VENDOR_ID;
  181. if(inl(SH7751_PCIREG_BASE+SH7751_PCICONF0) != id) {
  182. PCIDBG(2,"PCI: This is not an SH7751n");
  183. return NULL;
  184. }
  185. /*
  186.  * Check if configuration works.
  187.  */
  188. if (pci_probe & PCI_PROBE_CONF1) {
  189. tmp = inl (PCI_REG(SH7751_PCIPAR));
  190. outl (0x80000000, PCI_REG(SH7751_PCIPAR));
  191. if (inl (PCI_REG(SH7751_PCIPAR)) == 0x80000000) {
  192. outl (tmp, PCI_REG(SH7751_PCIPAR));
  193. printk(KERN_INFO "PCI: Using configuration type 1n");
  194. request_region(PCI_REG(SH7751_PCIPAR), 8, "PCI conf1");
  195. return &pci_direct_conf1;
  196. }
  197. outl (tmp, PCI_REG(SH7751_PCIPAR));
  198. }
  199. PCIDBG(2,"PCI: pci_check_direct failedn");
  200. return NULL;
  201. }
  202. #endif
  203. /*
  204.  * BIOS32 and PCI BIOS handling.
  205.  * 
  206.  * The BIOS version of the pci functions is not yet implemented but it is left
  207.  * in for completeness.  Currently an error will be generated at compile time. 
  208.  */
  209.  
  210. #ifdef CONFIG_PCI_BIOS
  211. #error PCI BIOS is not yet supported on SH7751
  212. #endif /* CONFIG_PCI_BIOS */
  213. /***************************************************************************************/
  214. /*
  215.  *  Handle bus scanning and fixups ....
  216.  */
  217. /*
  218.  * Discover remaining PCI buses in case there are peer host bridges.
  219.  * We use the number of last PCI bus provided by the PCI BIOS.
  220.  */
  221. static void __init pcibios_fixup_peer_bridges(void)
  222. {
  223. int n;
  224. struct pci_bus bus;
  225. struct pci_dev dev;
  226. u16 l;
  227. if (pcibios_last_bus <= 0 || pcibios_last_bus >= 0xff)
  228. return;
  229. PCIDBG(2,"PCI: Peer bridge fixupn");
  230. for (n=0; n <= pcibios_last_bus; n++) {
  231. if (pci_bus_exists(&pci_root_buses, n))
  232. continue;
  233. bus.number = n;
  234. bus.ops = pci_root_ops;
  235. dev.bus = &bus;
  236. for(dev.devfn=0; dev.devfn<256; dev.devfn += 8)
  237. if (!pci_read_config_word(&dev, PCI_VENDOR_ID, &l) &&
  238.     l != 0x0000 && l != 0xffff) {
  239. PCIDBG(3,"Found device at %02x:%02x [%04x]n", n, dev.devfn, l);
  240. printk(KERN_INFO "PCI: Discovered peer bus %02xn", n);
  241. pci_scan_bus(n, pci_root_ops, NULL);
  242. break;
  243. }
  244. }
  245. }
  246. static void __init pci_fixup_ide_bases(struct pci_dev *d)
  247. {
  248. int i;
  249. /*
  250.  * PCI IDE controllers use non-standard I/O port decoding, respect it.
  251.  */
  252. if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
  253. return;
  254. PCIDBG(3,"PCI: IDE base address fixup for %sn", d->slot_name);
  255. for(i=0; i<4; i++) {
  256. struct resource *r = &d->resource[i];
  257. if ((r->start & ~0x80) == 0x374) {
  258. r->start |= 2;
  259. r->end = r->start;
  260. }
  261. }
  262. }
  263. /* Add future fixups here... */
  264. struct pci_fixup pcibios_fixups[] = {
  265. { PCI_FIXUP_HEADER, PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases },
  266. { 0 }
  267. };
  268. void __init pcibios_fixup_pbus_ranges(struct pci_bus *b,
  269. struct pbus_set_ranges_data *range)
  270. {
  271. /* No fixups needed */
  272. }
  273. /*
  274.  *  Called after each bus is probed, but before its children
  275.  *  are examined.
  276.  */
  277. void __init pcibios_fixup_bus(struct pci_bus *b)
  278. {
  279. pci_read_bridge_bases(b);
  280. }
  281. /*
  282.  * Initialization. Try all known PCI access methods. Note that we support
  283.  * using both PCI BIOS and direct access: in such cases, we use I/O ports
  284.  * to access config space.
  285.  * 
  286.  * Note that the platform specific initialization (BSC registers, and memory
  287.  * space mapping) will be called via the machine vectors (sh_mv.mv_pci_init()) if it
  288.  * exitst and via the platform defined function pcibios_init_platform().  
  289.  * See pci_bigsur.c for implementation;
  290.  * 
  291.  * The BIOS version of the pci functions is not yet implemented but it is left
  292.  * in for completeness.  Currently an error will be genereated at compile time. 
  293.  */
  294. void __init pcibios_init(void)
  295. {
  296. struct pci_ops *bios = NULL;
  297. struct pci_ops *dir = NULL;
  298. PCIDBG(1,"PCI: Starting intialization.n");
  299. #ifdef CONFIG_PCI_BIOS
  300. if ((pci_probe & PCI_PROBE_BIOS) && ((bios = pci_find_bios()))) {
  301. pci_probe |= PCI_BIOS_SORT;
  302. pci_bios_present = 1;
  303. }
  304. #endif
  305. #ifdef CONFIG_PCI_DIRECT
  306. if (pci_probe & PCI_PROBE_CONF1 )
  307. dir = pci_check_direct();
  308. #endif
  309. if (dir) {
  310. pci_root_ops = dir;
  311.     if(!pcibios_init_platform())
  312. PCIDBG(1,"PCI: Initialization failedn");
  313.     if (sh_mv.mv_init_pci != NULL)
  314.             sh_mv.mv_init_pci();
  315. }
  316. else if (bios)
  317. pci_root_ops = bios;
  318. else {
  319. PCIDBG(1,"PCI: No PCI bus detectedn");
  320. return;
  321. }
  322. PCIDBG(1,"PCI: Probing PCI hardwaren");
  323. pci_root_bus = pci_scan_bus(0, pci_root_ops, NULL);
  324. //pci_assign_unassigned_resources();
  325. pci_fixup_irqs(pcibios_swizzle, pcibios_lookup_irq);
  326. pcibios_fixup_peer_bridges();
  327. pcibios_resource_survey();
  328. #ifdef CONFIG_PCI_BIOS
  329. if ((pci_probe & PCI_BIOS_SORT) && !(pci_probe & PCI_NO_SORT))
  330. pcibios_sort();
  331. #endif
  332. }
  333. char * __init pcibios_setup(char *str)
  334. {
  335. if (!strcmp(str, "off")) {
  336. pci_probe = 0;
  337. return NULL;
  338. }
  339. #ifdef CONFIG_PCI_BIOS
  340. else if (!strcmp(str, "bios")) {
  341. pci_probe = PCI_PROBE_BIOS;
  342. return NULL;
  343. } else if (!strcmp(str, "nobios")) {
  344. pci_probe &= ~PCI_PROBE_BIOS;
  345. return NULL;
  346. } else if (!strcmp(str, "nosort")) {
  347. pci_probe |= PCI_NO_SORT;
  348. return NULL;
  349. } else if (!strcmp(str, "biosirq")) {
  350. pci_probe |= PCI_BIOS_IRQ_SCAN;
  351. return NULL;
  352. }
  353. #endif
  354. #ifdef CONFIG_PCI_DIRECT
  355. else if (!strcmp(str, "conf1")) {
  356. pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
  357. return NULL;
  358. }
  359. #endif
  360. else if (!strcmp(str, "rom")) {
  361. pci_probe |= PCI_ASSIGN_ROMS;
  362. return NULL;
  363. } else if (!strncmp(str, "lastbus=", 8)) {
  364. pcibios_last_bus = simple_strtol(str+8, NULL, 0);
  365. return NULL;
  366. }
  367. return str;
  368. }
  369. /*
  370.  *    Allocate the bridge and device resources
  371.  */
  372. static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
  373. {
  374. struct list_head *ln;
  375. struct pci_bus *bus;
  376. struct pci_dev *dev;
  377. int idx;
  378. struct resource *r, *pr;
  379. PCIDBG(2,"PCI: pcibios_allocate_bus_reasources calledn" );
  380. /* Depth-First Search on bus tree */
  381. for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
  382. bus = pci_bus_b(ln);
  383. if ((dev = bus->self)) {
  384. for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
  385. r = &dev->resource[idx];
  386. if (!r->start)
  387. continue;
  388. pr = pci_find_parent_resource(dev, r);
  389. if (!pr || request_resource(pr, r) < 0)
  390. printk(KERN_ERR "PCI: Cannot allocate resource region %d of bridge %sn", idx, dev->slot_name);
  391. }
  392. }
  393. pcibios_allocate_bus_resources(&bus->children);
  394. }
  395. }
  396. static void __init pcibios_allocate_resources(int pass)
  397. {
  398. struct pci_dev *dev;
  399. int idx, disabled;
  400. u16 command;
  401. struct resource *r, *pr;
  402. PCIDBG(2,"PCI: pcibios_allocate_resources pass %d calledn", pass);
  403. pci_for_each_dev(dev) {
  404. pci_read_config_word(dev, PCI_COMMAND, &command);
  405. for(idx = 0; idx < 6; idx++) {
  406. r = &dev->resource[idx];
  407. if (r->parent) /* Already allocated */
  408. continue;
  409. if (!r->start) /* Address not assigned at all */
  410. continue;
  411. if (r->flags & IORESOURCE_IO)
  412. disabled = !(command & PCI_COMMAND_IO);
  413. else
  414. disabled = !(command & PCI_COMMAND_MEMORY);
  415. if (pass == disabled) {
  416. PCIDBG(3,"PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)n",
  417.     r->start, r->end, r->flags, disabled, pass);
  418. pr = pci_find_parent_resource(dev, r);
  419. if (!pr || request_resource(pr, r) < 0) {
  420. printk(KERN_ERR "PCI: Cannot allocate resource region %d of device %sn", idx, dev->slot_name);
  421. /* We'll assign a new address later */
  422. r->end -= r->start;
  423. r->start = 0;
  424. }
  425. }
  426. }
  427. if (!pass) {
  428. r = &dev->resource[PCI_ROM_RESOURCE];
  429. if (r->flags & PCI_ROM_ADDRESS_ENABLE) {
  430. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  431. u32 reg;
  432. PCIDBG(3,"PCI: Switching off ROM of %sn", dev->slot_name);
  433. r->flags &= ~PCI_ROM_ADDRESS_ENABLE;
  434. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  435. pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE);
  436. }
  437. }
  438. }
  439. }
  440. static void __init pcibios_assign_resources(void)
  441. {
  442. struct pci_dev *dev;
  443. int idx;
  444. struct resource *r;
  445. PCIDBG(2,"PCI: pcibios_assign_resources calledn");
  446. pci_for_each_dev(dev) {
  447. int class = dev->class >> 8;
  448. /* Don't touch classless devices and host bridges */
  449. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  450. continue;
  451. for(idx=0; idx<6; idx++) {
  452. r = &dev->resource[idx];
  453. /*
  454.  *  Don't touch IDE controllers and I/O ports of video cards!
  455.  */
  456. if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) ||
  457.     (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO)))
  458. continue;
  459. /*
  460.  *  We shall assign a new address to this resource, either because
  461.  *  the BIOS forgot to do so or because we have decided the old
  462.  *  address was unusable for some reason.
  463.  */
  464. if (!r->start && r->end)
  465. pci_assign_resource(dev, idx);
  466. }
  467. if (pci_probe & PCI_ASSIGN_ROMS) {
  468. r = &dev->resource[PCI_ROM_RESOURCE];
  469. r->end -= r->start;
  470. r->start = 0;
  471. if (r->end)
  472. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  473. }
  474. }
  475. }
  476. void __init pcibios_resource_survey(void)
  477. {
  478. PCIDBG(1,"PCI: Allocating resourcesn");
  479. pcibios_allocate_bus_resources(&pci_root_buses);
  480. pcibios_allocate_resources(0);
  481. pcibios_allocate_resources(1);
  482. pcibios_assign_resources();
  483. }
  484. /***************************************************************************************/
  485. /* 
  486.  *  IRQ functions 
  487.  */
  488. static u8 __init pcibios_swizzle(struct pci_dev *dev, u8 *pin)
  489. {
  490. /* no swizzling */
  491. return PCI_SLOT(dev->devfn);
  492. }
  493. static int pcibios_lookup_irq(struct pci_dev *dev, u8 slot, u8 pin)
  494. {
  495. int irq = -1;
  496. /* now lookup the actual IRQ on a platform specific basis (pci-'platform'.c) */
  497. irq = pcibios_map_platform_irq(slot,pin);
  498. if( irq < 0 ) {
  499.     PCIDBG(3,"PCI: Error mapping IRQ on device %sn", dev->name);
  500. return irq;
  501. }
  502. PCIDBG(2,"Setting IRQ for slot %s to %dn", dev->slot_name, irq);
  503. return irq;
  504. }