gelic_udbg.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * udbg debug output routine via GELIC UDP broadcasts
  3. *
  4. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  5. * Copyright 2006, 2007 Sony Corporation
  6. * Copyright (C) 2010 Hector Martin <hector@marcansoft.com>
  7. * Copyright (C) 2011 Andre Heider <a.heider@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. */
  15. #include <linux/if_ether.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/ip.h>
  19. #include <linux/udp.h>
  20. #include <asm/io.h>
  21. #include <asm/udbg.h>
  22. #include <asm/lv1call.h>
  23. #define GELIC_BUS_ID 1
  24. #define GELIC_DEVICE_ID 0
  25. #define GELIC_DEBUG_PORT 18194
  26. #define GELIC_MAX_MESSAGE_SIZE 1000
  27. #define GELIC_LV1_GET_MAC_ADDRESS 1
  28. #define GELIC_LV1_GET_VLAN_ID 4
  29. #define GELIC_LV1_VLAN_TX_ETHERNET_0 2
  30. #define GELIC_DESCR_DMA_STAT_MASK 0xf0000000
  31. #define GELIC_DESCR_DMA_CARDOWNED 0xa0000000
  32. #define GELIC_DESCR_TX_DMA_IKE 0x00080000
  33. #define GELIC_DESCR_TX_DMA_NO_CHKSUM 0x00000000
  34. #define GELIC_DESCR_TX_DMA_FRAME_TAIL 0x00040000
  35. #define GELIC_DESCR_DMA_CMD_NO_CHKSUM (GELIC_DESCR_DMA_CARDOWNED | \
  36. GELIC_DESCR_TX_DMA_IKE | \
  37. GELIC_DESCR_TX_DMA_NO_CHKSUM)
  38. static u64 bus_addr;
  39. struct gelic_descr {
  40. /* as defined by the hardware */
  41. __be32 buf_addr;
  42. __be32 buf_size;
  43. __be32 next_descr_addr;
  44. __be32 dmac_cmd_status;
  45. __be32 result_size;
  46. __be32 valid_size; /* all zeroes for tx */
  47. __be32 data_status;
  48. __be32 data_error; /* all zeroes for tx */
  49. } __attribute__((aligned(32)));
  50. struct debug_block {
  51. struct gelic_descr descr;
  52. u8 pkt[1520];
  53. } __packed;
  54. static __iomem struct ethhdr *h_eth;
  55. static __iomem struct vlan_hdr *h_vlan;
  56. static __iomem struct iphdr *h_ip;
  57. static __iomem struct udphdr *h_udp;
  58. static __iomem char *pmsg;
  59. static __iomem char *pmsgc;
  60. static __iomem struct debug_block dbg __attribute__((aligned(32)));
  61. static int header_size;
  62. static void map_dma_mem(int bus_id, int dev_id, void *start, size_t len,
  63. u64 *real_bus_addr)
  64. {
  65. s64 result;
  66. u64 real_addr = ((u64)start) & 0x0fffffffffffffffUL;
  67. u64 real_end = real_addr + len;
  68. u64 map_start = real_addr & ~0xfff;
  69. u64 map_end = (real_end + 0xfff) & ~0xfff;
  70. u64 bus_addr = 0;
  71. u64 flags = 0xf800000000000000UL;
  72. result = lv1_allocate_device_dma_region(bus_id, dev_id,
  73. map_end - map_start, 12, 0,
  74. &bus_addr);
  75. if (result)
  76. lv1_panic(0);
  77. result = lv1_map_device_dma_region(bus_id, dev_id, map_start,
  78. bus_addr, map_end - map_start,
  79. flags);
  80. if (result)
  81. lv1_panic(0);
  82. *real_bus_addr = bus_addr + real_addr - map_start;
  83. }
  84. static int unmap_dma_mem(int bus_id, int dev_id, u64 bus_addr, size_t len)
  85. {
  86. s64 result;
  87. u64 real_bus_addr;
  88. real_bus_addr = bus_addr & ~0xfff;
  89. len += bus_addr - real_bus_addr;
  90. len = (len + 0xfff) & ~0xfff;
  91. result = lv1_unmap_device_dma_region(bus_id, dev_id, real_bus_addr,
  92. len);
  93. if (result)
  94. return result;
  95. return lv1_free_device_dma_region(bus_id, dev_id, real_bus_addr);
  96. }
  97. static void gelic_debug_init(void)
  98. {
  99. s64 result;
  100. u64 v2;
  101. u64 mac;
  102. u64 vlan_id;
  103. result = lv1_open_device(GELIC_BUS_ID, GELIC_DEVICE_ID, 0);
  104. if (result)
  105. lv1_panic(0);
  106. map_dma_mem(GELIC_BUS_ID, GELIC_DEVICE_ID, &dbg, sizeof(dbg),
  107. &bus_addr);
  108. memset(&dbg, 0, sizeof(dbg));
  109. dbg.descr.buf_addr = bus_addr + offsetof(struct debug_block, pkt);
  110. wmb();
  111. result = lv1_net_control(GELIC_BUS_ID, GELIC_DEVICE_ID,
  112. GELIC_LV1_GET_MAC_ADDRESS, 0, 0, 0,
  113. &mac, &v2);
  114. if (result)
  115. lv1_panic(0);
  116. mac <<= 16;
  117. h_eth = (struct ethhdr *)dbg.pkt;
  118. eth_broadcast_addr(h_eth->h_dest);
  119. memcpy(&h_eth->h_source, &mac, ETH_ALEN);
  120. header_size = sizeof(struct ethhdr);
  121. result = lv1_net_control(GELIC_BUS_ID, GELIC_DEVICE_ID,
  122. GELIC_LV1_GET_VLAN_ID,
  123. GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
  124. &vlan_id, &v2);
  125. if (!result) {
  126. h_eth->h_proto= ETH_P_8021Q;
  127. header_size += sizeof(struct vlan_hdr);
  128. h_vlan = (struct vlan_hdr *)(h_eth + 1);
  129. h_vlan->h_vlan_TCI = vlan_id;
  130. h_vlan->h_vlan_encapsulated_proto = ETH_P_IP;
  131. h_ip = (struct iphdr *)(h_vlan + 1);
  132. } else {
  133. h_eth->h_proto= 0x0800;
  134. h_ip = (struct iphdr *)(h_eth + 1);
  135. }
  136. header_size += sizeof(struct iphdr);
  137. h_ip->version = 4;
  138. h_ip->ihl = 5;
  139. h_ip->ttl = 10;
  140. h_ip->protocol = 0x11;
  141. h_ip->saddr = 0x00000000;
  142. h_ip->daddr = 0xffffffff;
  143. header_size += sizeof(struct udphdr);
  144. h_udp = (struct udphdr *)(h_ip + 1);
  145. h_udp->source = GELIC_DEBUG_PORT;
  146. h_udp->dest = GELIC_DEBUG_PORT;
  147. pmsgc = pmsg = (char *)(h_udp + 1);
  148. }
  149. static void gelic_debug_shutdown(void)
  150. {
  151. if (bus_addr)
  152. unmap_dma_mem(GELIC_BUS_ID, GELIC_DEVICE_ID,
  153. bus_addr, sizeof(dbg));
  154. lv1_close_device(GELIC_BUS_ID, GELIC_DEVICE_ID);
  155. }
  156. static void gelic_sendbuf(int msgsize)
  157. {
  158. u16 *p;
  159. u32 sum;
  160. int i;
  161. dbg.descr.buf_size = header_size + msgsize;
  162. h_ip->tot_len = msgsize + sizeof(struct udphdr) +
  163. sizeof(struct iphdr);
  164. h_udp->len = msgsize + sizeof(struct udphdr);
  165. h_ip->check = 0;
  166. sum = 0;
  167. p = (u16 *)h_ip;
  168. for (i = 0; i < 5; i++)
  169. sum += *p++;
  170. h_ip->check = ~(sum + (sum >> 16));
  171. dbg.descr.dmac_cmd_status = GELIC_DESCR_DMA_CMD_NO_CHKSUM |
  172. GELIC_DESCR_TX_DMA_FRAME_TAIL;
  173. dbg.descr.result_size = 0;
  174. dbg.descr.data_status = 0;
  175. wmb();
  176. lv1_net_start_tx_dma(GELIC_BUS_ID, GELIC_DEVICE_ID, bus_addr, 0);
  177. while ((dbg.descr.dmac_cmd_status & GELIC_DESCR_DMA_STAT_MASK) ==
  178. GELIC_DESCR_DMA_CARDOWNED)
  179. cpu_relax();
  180. }
  181. static void ps3gelic_udbg_putc(char ch)
  182. {
  183. *pmsgc++ = ch;
  184. if (ch == '\n' || (pmsgc-pmsg) >= GELIC_MAX_MESSAGE_SIZE) {
  185. gelic_sendbuf(pmsgc-pmsg);
  186. pmsgc = pmsg;
  187. }
  188. }
  189. void __init udbg_init_ps3gelic(void)
  190. {
  191. gelic_debug_init();
  192. udbg_putc = ps3gelic_udbg_putc;
  193. }
  194. void udbg_shutdown_ps3gelic(void)
  195. {
  196. udbg_putc = NULL;
  197. gelic_debug_shutdown();
  198. }
  199. EXPORT_SYMBOL(udbg_shutdown_ps3gelic);