legacy_serial.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. #include <linux/kernel.h>
  2. #include <linux/serial.h>
  3. #include <linux/serial_8250.h>
  4. #include <linux/serial_core.h>
  5. #include <linux/console.h>
  6. #include <linux/pci.h>
  7. #include <asm/io.h>
  8. #include <asm/mmu.h>
  9. #include <asm/prom.h>
  10. #include <asm/serial.h>
  11. #include <asm/udbg.h>
  12. #include <asm/pci-bridge.h>
  13. #include <asm/ppc-pci.h>
  14. #undef DEBUG
  15. #ifdef DEBUG
  16. #define DBG(fmt...) do { printk(fmt); } while(0)
  17. #else
  18. #define DBG(fmt...) do { } while(0)
  19. #endif
  20. #define MAX_LEGACY_SERIAL_PORTS 8
  21. static struct plat_serial8250_port
  22. legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
  23. static struct legacy_serial_info {
  24. struct device_node *np;
  25. unsigned int speed;
  26. unsigned int clock;
  27. int irq_check_parent;
  28. phys_addr_t taddr;
  29. } legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
  30. static unsigned int legacy_serial_count;
  31. static int legacy_serial_console = -1;
  32. static int __init add_legacy_port(struct device_node *np, int want_index,
  33. int iotype, phys_addr_t base,
  34. phys_addr_t taddr, unsigned long irq,
  35. upf_t flags, int irq_check_parent)
  36. {
  37. u32 *clk, *spd, clock = BASE_BAUD * 16;
  38. int index;
  39. /* get clock freq. if present */
  40. clk = (u32 *)get_property(np, "clock-frequency", NULL);
  41. if (clk && *clk)
  42. clock = *clk;
  43. /* get default speed if present */
  44. spd = (u32 *)get_property(np, "current-speed", NULL);
  45. /* If we have a location index, then try to use it */
  46. if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
  47. index = want_index;
  48. else
  49. index = legacy_serial_count;
  50. /* if our index is still out of range, that mean that
  51. * array is full, we could scan for a free slot but that
  52. * make little sense to bother, just skip the port
  53. */
  54. if (index >= MAX_LEGACY_SERIAL_PORTS)
  55. return -1;
  56. if (index >= legacy_serial_count)
  57. legacy_serial_count = index + 1;
  58. /* Check if there is a port who already claimed our slot */
  59. if (legacy_serial_infos[index].np != 0) {
  60. /* if we still have some room, move it, else override */
  61. if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
  62. printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
  63. index, legacy_serial_count);
  64. legacy_serial_ports[legacy_serial_count] =
  65. legacy_serial_ports[index];
  66. legacy_serial_infos[legacy_serial_count] =
  67. legacy_serial_infos[index];
  68. legacy_serial_count++;
  69. } else {
  70. printk(KERN_DEBUG "Replacing legacy port %d\n", index);
  71. }
  72. }
  73. /* Now fill the entry */
  74. memset(&legacy_serial_ports[index], 0,
  75. sizeof(struct plat_serial8250_port));
  76. if (iotype == UPIO_PORT)
  77. legacy_serial_ports[index].iobase = base;
  78. else
  79. legacy_serial_ports[index].mapbase = base;
  80. legacy_serial_ports[index].iotype = iotype;
  81. legacy_serial_ports[index].uartclk = clock;
  82. legacy_serial_ports[index].irq = irq;
  83. legacy_serial_ports[index].flags = flags;
  84. legacy_serial_infos[index].taddr = taddr;
  85. legacy_serial_infos[index].np = of_node_get(np);
  86. legacy_serial_infos[index].clock = clock;
  87. legacy_serial_infos[index].speed = spd ? *spd : 0;
  88. legacy_serial_infos[index].irq_check_parent = irq_check_parent;
  89. printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
  90. index, np->full_name);
  91. printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
  92. (iotype == UPIO_PORT) ? "port" : "mem",
  93. (unsigned long long)base, (unsigned long long)taddr, irq,
  94. legacy_serial_ports[index].uartclk,
  95. legacy_serial_infos[index].speed);
  96. return index;
  97. }
  98. static int __init add_legacy_soc_port(struct device_node *np,
  99. struct device_node *soc_dev)
  100. {
  101. u64 addr;
  102. u32 *addrp;
  103. upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
  104. /* We only support ports that have a clock frequency properly
  105. * encoded in the device-tree.
  106. */
  107. if (get_property(np, "clock-frequency", NULL) == NULL)
  108. return -1;
  109. /* Get the address */
  110. addrp = of_get_address(soc_dev, 0, NULL, NULL);
  111. if (addrp == NULL)
  112. return -1;
  113. addr = of_translate_address(soc_dev, addrp);
  114. if (addr == OF_BAD_ADDR)
  115. return -1;
  116. /* Add port, irq will be dealt with later. We passed a translated
  117. * IO port value. It will be fixed up later along with the irq
  118. */
  119. return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0);
  120. }
  121. static int __init add_legacy_isa_port(struct device_node *np,
  122. struct device_node *isa_brg)
  123. {
  124. u32 *reg;
  125. char *typep;
  126. int index = -1;
  127. u64 taddr;
  128. DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
  129. /* Get the ISA port number */
  130. reg = (u32 *)get_property(np, "reg", NULL);
  131. if (reg == NULL)
  132. return -1;
  133. /* Verify it's an IO port, we don't support anything else */
  134. if (!(reg[0] & 0x00000001))
  135. return -1;
  136. /* Now look for an "ibm,aix-loc" property that gives us ordering
  137. * if any...
  138. */
  139. typep = (char *)get_property(np, "ibm,aix-loc", NULL);
  140. /* If we have a location index, then use it */
  141. if (typep && *typep == 'S')
  142. index = simple_strtol(typep+1, NULL, 0) - 1;
  143. /* Translate ISA address. If it fails, we still register the port
  144. * with no translated address so that it can be picked up as an IO
  145. * port later by the serial driver
  146. */
  147. taddr = of_translate_address(np, reg);
  148. if (taddr == OF_BAD_ADDR)
  149. taddr = 0;
  150. /* Add port, irq will be dealt with later */
  151. return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr,
  152. NO_IRQ, UPF_BOOT_AUTOCONF, 0);
  153. }
  154. #ifdef CONFIG_PCI
  155. static int __init add_legacy_pci_port(struct device_node *np,
  156. struct device_node *pci_dev)
  157. {
  158. u64 addr, base;
  159. u32 *addrp;
  160. unsigned int flags;
  161. int iotype, index = -1, lindex = 0;
  162. DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
  163. /* We only support ports that have a clock frequency properly
  164. * encoded in the device-tree (that is have an fcode). Anything
  165. * else can't be used that early and will be normally probed by
  166. * the generic 8250_pci driver later on. The reason is that 8250
  167. * compatible UARTs on PCI need all sort of quirks (port offsets
  168. * etc...) that this code doesn't know about
  169. */
  170. if (get_property(np, "clock-frequency", NULL) == NULL)
  171. return -1;
  172. /* Get the PCI address. Assume BAR 0 */
  173. addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
  174. if (addrp == NULL)
  175. return -1;
  176. /* We only support BAR 0 for now */
  177. iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
  178. addr = of_translate_address(pci_dev, addrp);
  179. if (addr == OF_BAD_ADDR)
  180. return -1;
  181. /* Set the IO base to the same as the translated address for MMIO,
  182. * or to the domain local IO base for PIO (it will be fixed up later)
  183. */
  184. if (iotype == UPIO_MEM)
  185. base = addr;
  186. else
  187. base = addrp[2];
  188. /* Try to guess an index... If we have subdevices of the pci dev,
  189. * we get to their "reg" property
  190. */
  191. if (np != pci_dev) {
  192. u32 *reg = (u32 *)get_property(np, "reg", NULL);
  193. if (reg && (*reg < 4))
  194. index = lindex = *reg;
  195. }
  196. /* Local index means it's the Nth port in the PCI chip. Unfortunately
  197. * the offset to add here is device specific. We know about those
  198. * EXAR ports and we default to the most common case. If your UART
  199. * doesn't work for these settings, you'll have to add your own special
  200. * cases here
  201. */
  202. if (device_is_compatible(pci_dev, "pci13a8,152") ||
  203. device_is_compatible(pci_dev, "pci13a8,154") ||
  204. device_is_compatible(pci_dev, "pci13a8,158")) {
  205. addr += 0x200 * lindex;
  206. base += 0x200 * lindex;
  207. } else {
  208. addr += 8 * lindex;
  209. base += 8 * lindex;
  210. }
  211. /* Add port, irq will be dealt with later. We passed a translated
  212. * IO port value. It will be fixed up later along with the irq
  213. */
  214. return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
  215. UPF_BOOT_AUTOCONF, np != pci_dev);
  216. }
  217. #endif
  218. static void __init setup_legacy_serial_console(int console)
  219. {
  220. struct legacy_serial_info *info =
  221. &legacy_serial_infos[console];
  222. void __iomem *addr;
  223. if (info->taddr == 0)
  224. return;
  225. addr = ioremap(info->taddr, 0x1000);
  226. if (addr == NULL)
  227. return;
  228. if (info->speed == 0)
  229. info->speed = udbg_probe_uart_speed(addr, info->clock);
  230. DBG("default console speed = %d\n", info->speed);
  231. udbg_init_uart(addr, info->speed, info->clock);
  232. }
  233. /*
  234. * This is called very early, as part of setup_system() or eventually
  235. * setup_arch(), basically before anything else in this file. This function
  236. * will try to build a list of all the available 8250-compatible serial ports
  237. * in the machine using the Open Firmware device-tree. It currently only deals
  238. * with ISA and PCI busses but could be extended. It allows a very early boot
  239. * console to be initialized, that list is also used later to provide 8250 with
  240. * the machine non-PCI ports and to properly pick the default console port
  241. */
  242. void __init find_legacy_serial_ports(void)
  243. {
  244. struct device_node *np, *stdout = NULL;
  245. char *path;
  246. int index;
  247. DBG(" -> find_legacy_serial_port()\n");
  248. /* Now find out if one of these is out firmware console */
  249. path = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
  250. if (path != NULL) {
  251. stdout = of_find_node_by_path(path);
  252. if (stdout)
  253. DBG("stdout is %s\n", stdout->full_name);
  254. } else {
  255. DBG(" no linux,stdout-path !\n");
  256. }
  257. /* First fill our array with SOC ports */
  258. for (np = NULL; (np = of_find_compatible_node(np, "serial", "ns16550")) != NULL;) {
  259. struct device_node *soc = of_get_parent(np);
  260. if (soc && !strcmp(soc->type, "soc")) {
  261. index = add_legacy_soc_port(np, np);
  262. if (index >= 0 && np == stdout)
  263. legacy_serial_console = index;
  264. }
  265. of_node_put(soc);
  266. }
  267. /* First fill our array with ISA ports */
  268. for (np = NULL; (np = of_find_node_by_type(np, "serial"));) {
  269. struct device_node *isa = of_get_parent(np);
  270. if (isa && !strcmp(isa->name, "isa")) {
  271. index = add_legacy_isa_port(np, isa);
  272. if (index >= 0 && np == stdout)
  273. legacy_serial_console = index;
  274. }
  275. of_node_put(isa);
  276. }
  277. /* First fill our array with tsi-bridge ports */
  278. for (np = NULL; (np = of_find_compatible_node(np, "serial", "ns16550")) != NULL;) {
  279. struct device_node *tsi = of_get_parent(np);
  280. if (tsi && !strcmp(tsi->type, "tsi-bridge")) {
  281. index = add_legacy_soc_port(np, np);
  282. if (index >= 0 && np == stdout)
  283. legacy_serial_console = index;
  284. }
  285. of_node_put(tsi);
  286. }
  287. #ifdef CONFIG_PCI
  288. /* Next, try to locate PCI ports */
  289. for (np = NULL; (np = of_find_all_nodes(np));) {
  290. struct device_node *pci, *parent = of_get_parent(np);
  291. if (parent && !strcmp(parent->name, "isa")) {
  292. of_node_put(parent);
  293. continue;
  294. }
  295. if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
  296. of_node_put(parent);
  297. continue;
  298. }
  299. /* Check for known pciclass, and also check wether we have
  300. * a device with child nodes for ports or not
  301. */
  302. if (device_is_compatible(np, "pciclass,0700") ||
  303. device_is_compatible(np, "pciclass,070002"))
  304. pci = np;
  305. else if (device_is_compatible(parent, "pciclass,0700") ||
  306. device_is_compatible(parent, "pciclass,070002"))
  307. pci = parent;
  308. else {
  309. of_node_put(parent);
  310. continue;
  311. }
  312. index = add_legacy_pci_port(np, pci);
  313. if (index >= 0 && np == stdout)
  314. legacy_serial_console = index;
  315. of_node_put(parent);
  316. }
  317. #endif
  318. DBG("legacy_serial_console = %d\n", legacy_serial_console);
  319. if (legacy_serial_console >= 0)
  320. setup_legacy_serial_console(legacy_serial_console);
  321. DBG(" <- find_legacy_serial_port()\n");
  322. }
  323. static struct platform_device serial_device = {
  324. .name = "serial8250",
  325. .id = PLAT8250_DEV_PLATFORM,
  326. .dev = {
  327. .platform_data = legacy_serial_ports,
  328. },
  329. };
  330. static void __init fixup_port_irq(int index,
  331. struct device_node *np,
  332. struct plat_serial8250_port *port)
  333. {
  334. unsigned int virq;
  335. DBG("fixup_port_irq(%d)\n", index);
  336. virq = irq_of_parse_and_map(np, 0);
  337. if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
  338. np = of_get_parent(np);
  339. if (np == NULL)
  340. return;
  341. virq = irq_of_parse_and_map(np, 0);
  342. of_node_put(np);
  343. }
  344. if (virq == NO_IRQ)
  345. return;
  346. port->irq = virq;
  347. }
  348. static void __init fixup_port_pio(int index,
  349. struct device_node *np,
  350. struct plat_serial8250_port *port)
  351. {
  352. #ifdef CONFIG_PCI
  353. struct pci_controller *hose;
  354. DBG("fixup_port_pio(%d)\n", index);
  355. hose = pci_find_hose_for_OF_device(np);
  356. if (hose) {
  357. unsigned long offset = (unsigned long)hose->io_base_virt -
  358. #ifdef CONFIG_PPC64
  359. pci_io_base;
  360. #else
  361. isa_io_base;
  362. #endif
  363. DBG("port %d, IO %lx -> %lx\n",
  364. index, port->iobase, port->iobase + offset);
  365. port->iobase += offset;
  366. }
  367. #endif
  368. }
  369. static void __init fixup_port_mmio(int index,
  370. struct device_node *np,
  371. struct plat_serial8250_port *port)
  372. {
  373. DBG("fixup_port_mmio(%d)\n", index);
  374. port->membase = ioremap(port->mapbase, 0x100);
  375. }
  376. /*
  377. * This is called as an arch initcall, hopefully before the PCI bus is
  378. * probed and/or the 8250 driver loaded since we need to register our
  379. * platform devices before 8250 PCI ones are detected as some of them
  380. * must properly "override" the platform ones.
  381. *
  382. * This function fixes up the interrupt value for platform ports as it
  383. * couldn't be done earlier before interrupt maps have been parsed. It
  384. * also "corrects" the IO address for PIO ports for the same reason,
  385. * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
  386. * registers all those platform ports for use by the 8250 driver when it
  387. * finally loads.
  388. */
  389. static int __init serial_dev_init(void)
  390. {
  391. int i;
  392. if (legacy_serial_count == 0)
  393. return -ENODEV;
  394. /*
  395. * Before we register the platfrom serial devices, we need
  396. * to fixup their interrutps and their IO ports.
  397. */
  398. DBG("Fixing serial ports interrupts and IO ports ...\n");
  399. for (i = 0; i < legacy_serial_count; i++) {
  400. struct plat_serial8250_port *port = &legacy_serial_ports[i];
  401. struct device_node *np = legacy_serial_infos[i].np;
  402. if (port->irq == NO_IRQ)
  403. fixup_port_irq(i, np, port);
  404. if (port->iotype == UPIO_PORT)
  405. fixup_port_pio(i, np, port);
  406. if (port->iotype == UPIO_MEM)
  407. fixup_port_mmio(i, np, port);
  408. }
  409. DBG("Registering platform serial ports\n");
  410. return platform_device_register(&serial_device);
  411. }
  412. arch_initcall(serial_dev_init);
  413. /*
  414. * This is called very early, as part of console_init() (typically just after
  415. * time_init()). This function is respondible for trying to find a good
  416. * default console on serial ports. It tries to match the open firmware
  417. * default output with one of the available serial console drivers, either
  418. * one of the platform serial ports that have been probed earlier by
  419. * find_legacy_serial_ports() or some more platform specific ones.
  420. */
  421. static int __init check_legacy_serial_console(void)
  422. {
  423. struct device_node *prom_stdout = NULL;
  424. int speed = 0, offset = 0;
  425. char *name;
  426. u32 *spd;
  427. DBG(" -> check_legacy_serial_console()\n");
  428. /* The user has requested a console so this is already set up. */
  429. if (strstr(saved_command_line, "console=")) {
  430. DBG(" console was specified !\n");
  431. return -EBUSY;
  432. }
  433. if (!of_chosen) {
  434. DBG(" of_chosen is NULL !\n");
  435. return -ENODEV;
  436. }
  437. if (legacy_serial_console < 0) {
  438. DBG(" legacy_serial_console not found !\n");
  439. return -ENODEV;
  440. }
  441. /* We are getting a weird phandle from OF ... */
  442. /* ... So use the full path instead */
  443. name = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
  444. if (name == NULL) {
  445. DBG(" no linux,stdout-path !\n");
  446. return -ENODEV;
  447. }
  448. prom_stdout = of_find_node_by_path(name);
  449. if (!prom_stdout) {
  450. DBG(" can't find stdout package %s !\n", name);
  451. return -ENODEV;
  452. }
  453. DBG("stdout is %s\n", prom_stdout->full_name);
  454. name = (char *)get_property(prom_stdout, "name", NULL);
  455. if (!name) {
  456. DBG(" stdout package has no name !\n");
  457. goto not_found;
  458. }
  459. spd = (u32 *)get_property(prom_stdout, "current-speed", NULL);
  460. if (spd)
  461. speed = *spd;
  462. if (0)
  463. ;
  464. #ifdef CONFIG_SERIAL_8250_CONSOLE
  465. else if (strcmp(name, "serial") == 0) {
  466. int i;
  467. /* Look for it in probed array */
  468. for (i = 0; i < legacy_serial_count; i++) {
  469. if (prom_stdout != legacy_serial_infos[i].np)
  470. continue;
  471. offset = i;
  472. speed = legacy_serial_infos[i].speed;
  473. break;
  474. }
  475. if (i >= legacy_serial_count)
  476. goto not_found;
  477. }
  478. #endif /* CONFIG_SERIAL_8250_CONSOLE */
  479. #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
  480. else if (strcmp(name, "ch-a") == 0)
  481. offset = 0;
  482. else if (strcmp(name, "ch-b") == 0)
  483. offset = 1;
  484. #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
  485. else
  486. goto not_found;
  487. of_node_put(prom_stdout);
  488. DBG("Found serial console at ttyS%d\n", offset);
  489. if (speed) {
  490. static char __initdata opt[16];
  491. sprintf(opt, "%d", speed);
  492. return add_preferred_console("ttyS", offset, opt);
  493. } else
  494. return add_preferred_console("ttyS", offset, NULL);
  495. not_found:
  496. DBG("No preferred console found !\n");
  497. of_node_put(prom_stdout);
  498. return -ENODEV;
  499. }
  500. console_initcall(check_legacy_serial_console);