legacy_serial.c 16 KB

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