Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * udbg debug output routine via GELIC UDP broadcasts
0004  *
0005  * Copyright (C) 2007 Sony Computer Entertainment Inc.
0006  * Copyright 2006, 2007 Sony Corporation
0007  * Copyright (C) 2010 Hector Martin <hector@marcansoft.com>
0008  * Copyright (C) 2011 Andre Heider <a.heider@gmail.com>
0009  */
0010 
0011 #include <linux/if_ether.h>
0012 #include <linux/etherdevice.h>
0013 #include <linux/if_vlan.h>
0014 #include <linux/ip.h>
0015 #include <linux/udp.h>
0016 
0017 #include <asm/io.h>
0018 #include <asm/udbg.h>
0019 #include <asm/lv1call.h>
0020 
0021 #define GELIC_BUS_ID 1
0022 #define GELIC_DEVICE_ID 0
0023 #define GELIC_DEBUG_PORT 18194
0024 #define GELIC_MAX_MESSAGE_SIZE 1000
0025 
0026 #define GELIC_LV1_GET_MAC_ADDRESS 1
0027 #define GELIC_LV1_GET_VLAN_ID 4
0028 #define GELIC_LV1_VLAN_TX_ETHERNET_0 2
0029 
0030 #define GELIC_DESCR_DMA_STAT_MASK 0xf0000000
0031 #define GELIC_DESCR_DMA_CARDOWNED 0xa0000000
0032 
0033 #define GELIC_DESCR_TX_DMA_IKE 0x00080000
0034 #define GELIC_DESCR_TX_DMA_NO_CHKSUM 0x00000000
0035 #define GELIC_DESCR_TX_DMA_FRAME_TAIL 0x00040000
0036 
0037 #define GELIC_DESCR_DMA_CMD_NO_CHKSUM (GELIC_DESCR_DMA_CARDOWNED | \
0038                        GELIC_DESCR_TX_DMA_IKE | \
0039                        GELIC_DESCR_TX_DMA_NO_CHKSUM)
0040 
0041 static u64 bus_addr;
0042 
0043 struct gelic_descr {
0044     /* as defined by the hardware */
0045     __be32 buf_addr;
0046     __be32 buf_size;
0047     __be32 next_descr_addr;
0048     __be32 dmac_cmd_status;
0049     __be32 result_size;
0050     __be32 valid_size;  /* all zeroes for tx */
0051     __be32 data_status;
0052     __be32 data_error;  /* all zeroes for tx */
0053 } __attribute__((aligned(32)));
0054 
0055 struct debug_block {
0056     struct gelic_descr descr;
0057     u8 pkt[1520];
0058 } __packed;
0059 
0060 static __iomem struct ethhdr *h_eth;
0061 static __iomem struct vlan_hdr *h_vlan;
0062 static __iomem struct iphdr *h_ip;
0063 static __iomem struct udphdr *h_udp;
0064 
0065 static __iomem char *pmsg;
0066 static __iomem char *pmsgc;
0067 
0068 static __iomem struct debug_block dbg __attribute__((aligned(32)));
0069 
0070 static int header_size;
0071 
0072 static void map_dma_mem(int bus_id, int dev_id, void *start, size_t len,
0073             u64 *real_bus_addr)
0074 {
0075     s64 result;
0076     u64 real_addr = ((u64)start) & 0x0fffffffffffffffUL;
0077     u64 real_end = real_addr + len;
0078     u64 map_start = real_addr & ~0xfff;
0079     u64 map_end = (real_end + 0xfff) & ~0xfff;
0080     u64 bus_addr = 0;
0081 
0082     u64 flags = 0xf800000000000000UL;
0083 
0084     result = lv1_allocate_device_dma_region(bus_id, dev_id,
0085                         map_end - map_start, 12, 0,
0086                         &bus_addr);
0087     if (result)
0088         lv1_panic(0);
0089 
0090     result = lv1_map_device_dma_region(bus_id, dev_id, map_start,
0091                        bus_addr, map_end - map_start,
0092                        flags);
0093     if (result)
0094         lv1_panic(0);
0095 
0096     *real_bus_addr = bus_addr + real_addr - map_start;
0097 }
0098 
0099 static int unmap_dma_mem(int bus_id, int dev_id, u64 bus_addr, size_t len)
0100 {
0101     s64 result;
0102     u64 real_bus_addr;
0103 
0104     real_bus_addr = bus_addr & ~0xfff;
0105     len += bus_addr - real_bus_addr;
0106     len = (len + 0xfff) & ~0xfff;
0107 
0108     result = lv1_unmap_device_dma_region(bus_id, dev_id, real_bus_addr,
0109                          len);
0110     if (result)
0111         return result;
0112 
0113     return lv1_free_device_dma_region(bus_id, dev_id, real_bus_addr);
0114 }
0115 
0116 static void __init gelic_debug_init(void)
0117 {
0118     s64 result;
0119     u64 v2;
0120     u64 mac;
0121     u64 vlan_id;
0122 
0123     result = lv1_open_device(GELIC_BUS_ID, GELIC_DEVICE_ID, 0);
0124     if (result)
0125         lv1_panic(0);
0126 
0127     map_dma_mem(GELIC_BUS_ID, GELIC_DEVICE_ID, &dbg, sizeof(dbg),
0128             &bus_addr);
0129 
0130     memset(&dbg, 0, sizeof(dbg));
0131 
0132     dbg.descr.buf_addr = bus_addr + offsetof(struct debug_block, pkt);
0133 
0134     wmb();
0135 
0136     result = lv1_net_control(GELIC_BUS_ID, GELIC_DEVICE_ID,
0137                  GELIC_LV1_GET_MAC_ADDRESS, 0, 0, 0,
0138                  &mac, &v2);
0139     if (result)
0140         lv1_panic(0);
0141 
0142     mac <<= 16;
0143 
0144     h_eth = (struct ethhdr *)dbg.pkt;
0145 
0146     eth_broadcast_addr(h_eth->h_dest);
0147     memcpy(&h_eth->h_source, &mac, ETH_ALEN);
0148 
0149     header_size = sizeof(struct ethhdr);
0150 
0151     result = lv1_net_control(GELIC_BUS_ID, GELIC_DEVICE_ID,
0152                  GELIC_LV1_GET_VLAN_ID,
0153                  GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
0154                  &vlan_id, &v2);
0155     if (!result) {
0156         h_eth->h_proto= ETH_P_8021Q;
0157 
0158         header_size += sizeof(struct vlan_hdr);
0159         h_vlan = (struct vlan_hdr *)(h_eth + 1);
0160         h_vlan->h_vlan_TCI = vlan_id;
0161         h_vlan->h_vlan_encapsulated_proto = ETH_P_IP;
0162         h_ip = (struct iphdr *)(h_vlan + 1);
0163     } else {
0164         h_eth->h_proto= 0x0800;
0165         h_ip = (struct iphdr *)(h_eth + 1);
0166     }
0167 
0168     header_size += sizeof(struct iphdr);
0169     h_ip->version = 4;
0170     h_ip->ihl = 5;
0171     h_ip->ttl = 10;
0172     h_ip->protocol = 0x11;
0173     h_ip->saddr = 0x00000000;
0174     h_ip->daddr = 0xffffffff;
0175 
0176     header_size += sizeof(struct udphdr);
0177     h_udp = (struct udphdr *)(h_ip + 1);
0178     h_udp->source = GELIC_DEBUG_PORT;
0179     h_udp->dest = GELIC_DEBUG_PORT;
0180 
0181     pmsgc = pmsg = (char *)(h_udp + 1);
0182 }
0183 
0184 static void gelic_debug_shutdown(void)
0185 {
0186     if (bus_addr)
0187         unmap_dma_mem(GELIC_BUS_ID, GELIC_DEVICE_ID,
0188                   bus_addr, sizeof(dbg));
0189     lv1_close_device(GELIC_BUS_ID, GELIC_DEVICE_ID);
0190 }
0191 
0192 static void gelic_sendbuf(int msgsize)
0193 {
0194     u16 *p;
0195     u32 sum;
0196     int i;
0197 
0198     dbg.descr.buf_size = header_size + msgsize;
0199     h_ip->tot_len = msgsize + sizeof(struct udphdr) +
0200                  sizeof(struct iphdr);
0201     h_udp->len = msgsize + sizeof(struct udphdr);
0202 
0203     h_ip->check = 0;
0204     sum = 0;
0205     p = (u16 *)h_ip;
0206     for (i = 0; i < 5; i++)
0207         sum += *p++;
0208     h_ip->check = ~(sum + (sum >> 16));
0209 
0210     dbg.descr.dmac_cmd_status = GELIC_DESCR_DMA_CMD_NO_CHKSUM |
0211                     GELIC_DESCR_TX_DMA_FRAME_TAIL;
0212     dbg.descr.result_size = 0;
0213     dbg.descr.data_status = 0;
0214 
0215     wmb();
0216 
0217     lv1_net_start_tx_dma(GELIC_BUS_ID, GELIC_DEVICE_ID, bus_addr, 0);
0218 
0219     while ((dbg.descr.dmac_cmd_status & GELIC_DESCR_DMA_STAT_MASK) ==
0220            GELIC_DESCR_DMA_CARDOWNED)
0221         cpu_relax();
0222 }
0223 
0224 static void ps3gelic_udbg_putc(char ch)
0225 {
0226     *pmsgc++ = ch;
0227     if (ch == '\n' || (pmsgc-pmsg) >= GELIC_MAX_MESSAGE_SIZE) {
0228         gelic_sendbuf(pmsgc-pmsg);
0229         pmsgc = pmsg;
0230     }
0231 }
0232 
0233 void __init udbg_init_ps3gelic(void)
0234 {
0235     gelic_debug_init();
0236     udbg_putc = ps3gelic_udbg_putc;
0237 }
0238 
0239 void udbg_shutdown_ps3gelic(void)
0240 {
0241     udbg_putc = NULL;
0242     gelic_debug_shutdown();
0243 }
0244 EXPORT_SYMBOL(udbg_shutdown_ps3gelic);