legacy_serial.c 14 KB

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