Scott Wood | d0f53fa | 2007-08-21 03:39:57 +1000 | [diff] [blame] | 1 | /* |
| 2 | * CPM serial console support. |
| 3 | * |
| 4 | * Copyright 2007 Freescale Semiconductor, Inc. |
| 5 | * Author: Scott Wood <scottwood@freescale.com> |
| 6 | * |
| 7 | * It is assumed that the firmware (or the platform file) has already set |
| 8 | * up the port. |
| 9 | */ |
| 10 | |
| 11 | #include "types.h" |
| 12 | #include "io.h" |
| 13 | #include "ops.h" |
| 14 | |
| 15 | struct cpm_scc { |
| 16 | u32 gsmrl; |
| 17 | u32 gsmrh; |
| 18 | u16 psmr; |
| 19 | u8 res1[2]; |
| 20 | u16 todr; |
| 21 | u16 dsr; |
| 22 | u16 scce; |
| 23 | u8 res2[2]; |
| 24 | u16 sccm; |
| 25 | u8 res3; |
| 26 | u8 sccs; |
| 27 | u8 res4[8]; |
| 28 | }; |
| 29 | |
| 30 | struct cpm_smc { |
| 31 | u8 res1[2]; |
| 32 | u16 smcmr; |
| 33 | u8 res2[2]; |
| 34 | u8 smce; |
| 35 | u8 res3[3]; |
| 36 | u8 smcm; |
| 37 | u8 res4[5]; |
| 38 | }; |
| 39 | |
| 40 | struct cpm_param { |
| 41 | u16 rbase; |
| 42 | u16 tbase; |
| 43 | u8 rfcr; |
| 44 | u8 tfcr; |
| 45 | }; |
| 46 | |
| 47 | struct cpm_bd { |
| 48 | u16 sc; /* Status and Control */ |
| 49 | u16 len; /* Data length in buffer */ |
| 50 | u8 *addr; /* Buffer address in host memory */ |
| 51 | }; |
| 52 | |
| 53 | static void *cpcr; |
| 54 | static struct cpm_param *param; |
| 55 | static struct cpm_smc *smc; |
| 56 | static struct cpm_scc *scc; |
| 57 | struct cpm_bd *tbdf, *rbdf; |
| 58 | static u32 cpm_cmd; |
| 59 | static u8 *dpram_start; |
| 60 | |
| 61 | static void (*do_cmd)(int op); |
| 62 | static void (*enable_port)(void); |
| 63 | static void (*disable_port)(void); |
| 64 | |
| 65 | #define CPM_CMD_STOP_TX 4 |
| 66 | #define CPM_CMD_RESTART_TX 6 |
| 67 | #define CPM_CMD_INIT_RX_TX 0 |
| 68 | |
| 69 | static void cpm1_cmd(int op) |
| 70 | { |
| 71 | while (in_be16(cpcr) & 1) |
| 72 | ; |
| 73 | |
| 74 | out_be16(cpcr, (op << 8) | cpm_cmd | 1); |
| 75 | |
| 76 | while (in_be16(cpcr) & 1) |
| 77 | ; |
| 78 | } |
| 79 | |
| 80 | static void cpm2_cmd(int op) |
| 81 | { |
| 82 | while (in_be32(cpcr) & 0x10000) |
| 83 | ; |
| 84 | |
| 85 | out_be32(cpcr, op | cpm_cmd | 0x10000); |
| 86 | |
| 87 | while (in_be32(cpcr) & 0x10000) |
| 88 | ; |
| 89 | } |
| 90 | |
| 91 | static void smc_disable_port(void) |
| 92 | { |
| 93 | do_cmd(CPM_CMD_STOP_TX); |
| 94 | out_be16(&smc->smcmr, in_be16(&smc->smcmr) & ~3); |
| 95 | } |
| 96 | |
| 97 | static void scc_disable_port(void) |
| 98 | { |
| 99 | do_cmd(CPM_CMD_STOP_TX); |
| 100 | out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) & ~0x30); |
| 101 | } |
| 102 | |
| 103 | static void smc_enable_port(void) |
| 104 | { |
| 105 | out_be16(&smc->smcmr, in_be16(&smc->smcmr) | 3); |
| 106 | do_cmd(CPM_CMD_RESTART_TX); |
| 107 | } |
| 108 | |
| 109 | static void scc_enable_port(void) |
| 110 | { |
| 111 | out_be32(&scc->gsmrl, in_be32(&scc->gsmrl) | 0x30); |
| 112 | do_cmd(CPM_CMD_RESTART_TX); |
| 113 | } |
| 114 | |
| 115 | static int cpm_serial_open(void) |
| 116 | { |
| 117 | int dpaddr = 0x800; |
| 118 | disable_port(); |
| 119 | |
| 120 | out_8(¶m->rfcr, 0x10); |
| 121 | out_8(¶m->tfcr, 0x10); |
| 122 | |
| 123 | rbdf = (struct cpm_bd *)(dpram_start + dpaddr); |
| 124 | rbdf->addr = (u8 *)(rbdf + 2); |
| 125 | rbdf->sc = 0xa000; |
| 126 | rbdf->len = 1; |
| 127 | |
| 128 | tbdf = rbdf + 1; |
| 129 | tbdf->addr = (u8 *)(rbdf + 2) + 1; |
| 130 | tbdf->sc = 0x2000; |
| 131 | tbdf->len = 1; |
| 132 | |
| 133 | sync(); |
| 134 | out_be16(¶m->rbase, dpaddr); |
| 135 | out_be16(¶m->tbase, dpaddr + sizeof(struct cpm_bd)); |
| 136 | |
| 137 | do_cmd(CPM_CMD_INIT_RX_TX); |
| 138 | |
| 139 | enable_port(); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static void cpm_serial_putc(unsigned char c) |
| 144 | { |
| 145 | while (tbdf->sc & 0x8000) |
| 146 | barrier(); |
| 147 | |
| 148 | sync(); |
| 149 | |
| 150 | tbdf->addr[0] = c; |
| 151 | eieio(); |
| 152 | tbdf->sc |= 0x8000; |
| 153 | } |
| 154 | |
| 155 | static unsigned char cpm_serial_tstc(void) |
| 156 | { |
| 157 | barrier(); |
| 158 | return !(rbdf->sc & 0x8000); |
| 159 | } |
| 160 | |
| 161 | static unsigned char cpm_serial_getc(void) |
| 162 | { |
| 163 | unsigned char c; |
| 164 | |
| 165 | while (!cpm_serial_tstc()) |
| 166 | ; |
| 167 | |
| 168 | sync(); |
| 169 | c = rbdf->addr[0]; |
| 170 | eieio(); |
| 171 | rbdf->sc |= 0x8000; |
| 172 | |
| 173 | return c; |
| 174 | } |
| 175 | |
| 176 | int cpm_console_init(void *devp, struct serial_console_data *scdp) |
| 177 | { |
| 178 | void *reg_virt[2]; |
| 179 | int is_smc = 0, is_cpm2 = 0, n; |
| 180 | unsigned long reg_phys; |
| 181 | void *parent; |
| 182 | |
| 183 | if (dt_is_compatible(devp, "fsl,cpm1-smc-uart")) { |
| 184 | is_smc = 1; |
| 185 | } else if (dt_is_compatible(devp, "fsl,cpm2-scc-uart")) { |
| 186 | is_cpm2 = 1; |
| 187 | } else if (dt_is_compatible(devp, "fsl,cpm2-smc-uart")) { |
| 188 | is_cpm2 = 1; |
| 189 | is_smc = 1; |
| 190 | } |
| 191 | |
| 192 | if (is_smc) { |
| 193 | enable_port = smc_enable_port; |
| 194 | disable_port = smc_disable_port; |
| 195 | } else { |
| 196 | enable_port = scc_enable_port; |
| 197 | disable_port = scc_disable_port; |
| 198 | } |
| 199 | |
| 200 | if (is_cpm2) |
| 201 | do_cmd = cpm2_cmd; |
| 202 | else |
| 203 | do_cmd = cpm1_cmd; |
| 204 | |
| 205 | n = getprop(devp, "fsl,cpm-command", &cpm_cmd, 4); |
| 206 | if (n < 4) |
| 207 | return -1; |
| 208 | |
| 209 | n = getprop(devp, "virtual-reg", reg_virt, sizeof(reg_virt)); |
| 210 | if (n < (int)sizeof(reg_virt)) { |
| 211 | for (n = 0; n < 2; n++) { |
| 212 | if (!dt_xlate_reg(devp, n, ®_phys, NULL)) |
| 213 | return -1; |
| 214 | |
| 215 | reg_virt[n] = (void *)reg_phys; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (is_smc) |
| 220 | smc = reg_virt[0]; |
| 221 | else |
| 222 | scc = reg_virt[0]; |
| 223 | |
| 224 | param = reg_virt[1]; |
| 225 | |
| 226 | parent = get_parent(devp); |
| 227 | if (!parent) |
| 228 | return -1; |
| 229 | |
| 230 | n = getprop(parent, "virtual-reg", reg_virt, sizeof(reg_virt)); |
| 231 | if (n < (int)sizeof(reg_virt)) { |
| 232 | for (n = 0; n < 2; n++) { |
| 233 | if (!dt_xlate_reg(parent, n, ®_phys, NULL)) |
| 234 | return -1; |
| 235 | |
| 236 | reg_virt[n] = (void *)reg_phys; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | cpcr = reg_virt[0]; |
| 241 | dpram_start = reg_virt[1]; |
| 242 | |
| 243 | scdp->open = cpm_serial_open; |
| 244 | scdp->putc = cpm_serial_putc; |
| 245 | scdp->getc = cpm_serial_getc; |
| 246 | scdp->tstc = cpm_serial_tstc; |
| 247 | |
| 248 | return 0; |
| 249 | } |