ucc_slow.c 9.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2006 Freescale Semicondutor, Inc. All rights reserved.
 *
 * Authors: 	Shlomi Gridish <gridish@freescale.com>
 * 		Li Yang <leoli@freescale.com>
 *
 * Description:
 * QE UCC Slow API Set - UCC Slow specific routines implementations.
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/stddef.h>
#include <linux/interrupt.h>
21
#include <linux/err.h>
22
#include <linux/module.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

#include <asm/io.h>
#include <asm/immap_qe.h>
#include <asm/qe.h>

#include <asm/ucc.h>
#include <asm/ucc_slow.h>

u32 ucc_slow_get_qe_cr_subblock(int uccs_num)
{
	switch (uccs_num) {
	case 0: return QE_CR_SUBBLOCK_UCCSLOW1;
	case 1: return QE_CR_SUBBLOCK_UCCSLOW2;
	case 2: return QE_CR_SUBBLOCK_UCCSLOW3;
	case 3: return QE_CR_SUBBLOCK_UCCSLOW4;
	case 4: return QE_CR_SUBBLOCK_UCCSLOW5;
	case 5: return QE_CR_SUBBLOCK_UCCSLOW6;
	case 6: return QE_CR_SUBBLOCK_UCCSLOW7;
	case 7: return QE_CR_SUBBLOCK_UCCSLOW8;
	default: return QE_CR_SUBBLOCK_INVALID;
	}
}
45
EXPORT_SYMBOL(ucc_slow_get_qe_cr_subblock);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

void ucc_slow_poll_transmitter_now(struct ucc_slow_private * uccs)
{
	out_be16(&uccs->us_regs->utodr, UCC_SLOW_TOD);
}

void ucc_slow_graceful_stop_tx(struct ucc_slow_private * uccs)
{
	struct ucc_slow_info *us_info = uccs->us_info;
	u32 id;

	id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
	qe_issue_cmd(QE_GRACEFUL_STOP_TX, id,
			 QE_CR_PROTOCOL_UNSPECIFIED, 0);
}
61
EXPORT_SYMBOL(ucc_slow_graceful_stop_tx);
62 63 64 65 66 67 68 69 70

void ucc_slow_stop_tx(struct ucc_slow_private * uccs)
{
	struct ucc_slow_info *us_info = uccs->us_info;
	u32 id;

	id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
	qe_issue_cmd(QE_STOP_TX, id, QE_CR_PROTOCOL_UNSPECIFIED, 0);
}
71
EXPORT_SYMBOL(ucc_slow_stop_tx);
72 73 74 75 76 77 78 79 80

void ucc_slow_restart_tx(struct ucc_slow_private * uccs)
{
	struct ucc_slow_info *us_info = uccs->us_info;
	u32 id;

	id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
	qe_issue_cmd(QE_RESTART_TX, id, QE_CR_PROTOCOL_UNSPECIFIED, 0);
}
81
EXPORT_SYMBOL(ucc_slow_restart_tx);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

void ucc_slow_enable(struct ucc_slow_private * uccs, enum comm_dir mode)
{
	struct ucc_slow *us_regs;
	u32 gumr_l;

	us_regs = uccs->us_regs;

	/* Enable reception and/or transmission on this UCC. */
	gumr_l = in_be32(&us_regs->gumr_l);
	if (mode & COMM_DIR_TX) {
		gumr_l |= UCC_SLOW_GUMR_L_ENT;
		uccs->enabled_tx = 1;
	}
	if (mode & COMM_DIR_RX) {
		gumr_l |= UCC_SLOW_GUMR_L_ENR;
		uccs->enabled_rx = 1;
	}
	out_be32(&us_regs->gumr_l, gumr_l);
}
102
EXPORT_SYMBOL(ucc_slow_enable);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

void ucc_slow_disable(struct ucc_slow_private * uccs, enum comm_dir mode)
{
	struct ucc_slow *us_regs;
	u32 gumr_l;

	us_regs = uccs->us_regs;

	/* Disable reception and/or transmission on this UCC. */
	gumr_l = in_be32(&us_regs->gumr_l);
	if (mode & COMM_DIR_TX) {
		gumr_l &= ~UCC_SLOW_GUMR_L_ENT;
		uccs->enabled_tx = 0;
	}
	if (mode & COMM_DIR_RX) {
		gumr_l &= ~UCC_SLOW_GUMR_L_ENR;
		uccs->enabled_rx = 0;
	}
	out_be32(&us_regs->gumr_l, gumr_l);
}
123
EXPORT_SYMBOL(ucc_slow_disable);
124

125 126 127 128
/* Initialize the UCC for Slow operations
 *
 * The caller should initialize the following us_info
 */
129 130
int ucc_slow_init(struct ucc_slow_info * us_info, struct ucc_slow_private ** uccs_ret)
{
131
	struct ucc_slow_private *uccs;
132
	u32 i;
133
	struct ucc_slow __iomem *us_regs;
134
	u32 gumr;
135
	struct qe_bd *bd;
136 137
	u32 id;
	u32 command;
138
	int ret = 0;
139 140 141 142 143 144

	if (!us_info)
		return -EINVAL;

	/* check if the UCC port number is in range. */
	if ((us_info->ucc_num < 0) || (us_info->ucc_num > UCC_MAX_NUM - 1)) {
145
		printk(KERN_ERR "%s: illegal UCC number\n", __FUNCTION__);
146 147 148 149 150 151
		return -EINVAL;
	}

	/*
	 * Set mrblr
	 * Check that 'max_rx_buf_length' is properly aligned (4), unless
152
	 * rfw is 1, meaning that QE accepts one byte at a time, unlike normal
153 154 155 156
	 * case when QE accepts 32 bits at a time.
	 */
	if ((!us_info->rfw) &&
		(us_info->max_rx_buf_length & (UCC_SLOW_MRBLR_ALIGNMENT - 1))) {
157
		printk(KERN_ERR "max_rx_buf_length not aligned.\n");
158 159 160
		return -EINVAL;
	}

161
	uccs = kzalloc(sizeof(struct ucc_slow_private), GFP_KERNEL);
162
	if (!uccs) {
163 164
		printk(KERN_ERR "%s: Cannot allocate private data\n",
			__FUNCTION__);
165 166 167 168 169
		return -ENOMEM;
	}

	/* Fill slow UCC structure */
	uccs->us_info = us_info;
170 171 172
	/* Set the PHY base address */
	uccs->us_regs = ioremap(us_info->regs, sizeof(struct ucc_slow));
	if (uccs->us_regs == NULL) {
173
		printk(KERN_ERR "%s: Cannot map UCC registers\n", __FUNCTION__);
174 175 176
		return -ENOMEM;
	}

177 178 179 180 181 182 183 184 185 186 187 188
	uccs->saved_uccm = 0;
	uccs->p_rx_frame = 0;
	us_regs = uccs->us_regs;
	uccs->p_ucce = (u16 *) & (us_regs->ucce);
	uccs->p_uccm = (u16 *) & (us_regs->uccm);
#ifdef STATISTICS
	uccs->rx_frames = 0;
	uccs->tx_frames = 0;
	uccs->rx_discarded = 0;
#endif				/* STATISTICS */

	/* Get PRAM base */
189 190
	uccs->us_pram_offset =
		qe_muram_alloc(UCC_SLOW_PRAM_SIZE, ALIGNMENT_OF_UCC_SLOW_PRAM);
191
	if (IS_ERR_VALUE(uccs->us_pram_offset)) {
192
		printk(KERN_ERR "%s: cannot allocate MURAM for PRAM", __FUNCTION__);
193 194 195 196
		ucc_slow_free(uccs);
		return -ENOMEM;
	}
	id = ucc_slow_get_qe_cr_subblock(us_info->ucc_num);
197
	qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, id, us_info->protocol,
198
		     uccs->us_pram_offset);
199 200 201 202

	uccs->us_pram = qe_muram_addr(uccs->us_pram_offset);

	/* Set UCC to slow type */
203 204
	ret = ucc_set_type(us_info->ucc_num, UCC_SPEED_TYPE_SLOW);
	if (ret) {
205
		printk(KERN_ERR "%s: cannot set UCC type", __FUNCTION__);
206 207 208 209 210 211 212 213 214 215 216 217
		ucc_slow_free(uccs);
		return ret;
	}

	out_be16(&uccs->us_pram->mrblr, us_info->max_rx_buf_length);

	INIT_LIST_HEAD(&uccs->confQ);

	/* Allocate BDs. */
	uccs->rx_base_offset =
		qe_muram_alloc(us_info->rx_bd_ring_len * sizeof(struct qe_bd),
				QE_ALIGNMENT_OF_BD);
218
	if (IS_ERR_VALUE(uccs->rx_base_offset)) {
219 220
		printk(KERN_ERR "%s: cannot allocate %u RX BDs\n", __FUNCTION__,
			us_info->rx_bd_ring_len);
221 222 223 224 225 226 227 228
		uccs->rx_base_offset = 0;
		ucc_slow_free(uccs);
		return -ENOMEM;
	}

	uccs->tx_base_offset =
		qe_muram_alloc(us_info->tx_bd_ring_len * sizeof(struct qe_bd),
			QE_ALIGNMENT_OF_BD);
229
	if (IS_ERR_VALUE(uccs->tx_base_offset)) {
230
		printk(KERN_ERR "%s: cannot allocate TX BDs", __FUNCTION__);
231 232 233 234 235 236 237
		uccs->tx_base_offset = 0;
		ucc_slow_free(uccs);
		return -ENOMEM;
	}

	/* Init Tx bds */
	bd = uccs->confBd = uccs->tx_bd = qe_muram_addr(uccs->tx_base_offset);
238
	for (i = 0; i < us_info->tx_bd_ring_len - 1; i++) {
239
		/* clear bd buffer */
240
		out_be32(&bd->buf, 0);
241
		/* set bd status and length */
242 243
		out_be32((u32 *) bd, 0);
		bd++;
244
	}
245 246 247
	/* for last BD set Wrap bit */
	out_be32(&bd->buf, 0);
	out_be32((u32 *) bd, cpu_to_be32(T_W));
248 249 250

	/* Init Rx bds */
	bd = uccs->rx_bd = qe_muram_addr(uccs->rx_base_offset);
251
	for (i = 0; i < us_info->rx_bd_ring_len - 1; i++) {
252 253 254
		/* set bd status and length */
		out_be32((u32*)bd, 0);
		/* clear bd buffer */
255 256
		out_be32(&bd->buf, 0);
		bd++;
257
	}
258 259 260
	/* for last BD set Wrap bit */
	out_be32((u32*)bd, cpu_to_be32(R_W));
	out_be32(&bd->buf, 0);
261 262 263

	/* Set GUMR (For more details see the hardware spec.). */
	/* gumr_h */
264
	gumr = us_info->tcrc;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	if (us_info->cdp)
		gumr |= UCC_SLOW_GUMR_H_CDP;
	if (us_info->ctsp)
		gumr |= UCC_SLOW_GUMR_H_CTSP;
	if (us_info->cds)
		gumr |= UCC_SLOW_GUMR_H_CDS;
	if (us_info->ctss)
		gumr |= UCC_SLOW_GUMR_H_CTSS;
	if (us_info->tfl)
		gumr |= UCC_SLOW_GUMR_H_TFL;
	if (us_info->rfw)
		gumr |= UCC_SLOW_GUMR_H_RFW;
	if (us_info->txsy)
		gumr |= UCC_SLOW_GUMR_H_TXSY;
	if (us_info->rtsm)
		gumr |= UCC_SLOW_GUMR_H_RTSM;
	out_be32(&us_regs->gumr_h, gumr);

	/* gumr_l */
284 285
	gumr = us_info->tdcr | us_info->rdcr | us_info->tenc | us_info->renc |
		us_info->diag | us_info->mode;
286 287 288 289 290 291 292 293 294 295 296 297 298 299
	if (us_info->tci)
		gumr |= UCC_SLOW_GUMR_L_TCI;
	if (us_info->rinv)
		gumr |= UCC_SLOW_GUMR_L_RINV;
	if (us_info->tinv)
		gumr |= UCC_SLOW_GUMR_L_TINV;
	if (us_info->tend)
		gumr |= UCC_SLOW_GUMR_L_TEND;
	out_be32(&us_regs->gumr_l, gumr);

	/* Function code registers */

	/* if the data is in cachable memory, the 'global' */
	/* in the function code should be set. */
300 301
	uccs->us_pram->tbmr = UCC_BMR_BO_BE;
	uccs->us_pram->rbmr = UCC_BMR_BO_BE;
302 303

	/* rbase, tbase are offsets from MURAM base */
304 305
	out_be16(&uccs->us_pram->rbase, uccs->rx_base_offset);
	out_be16(&uccs->us_pram->tbase, uccs->tx_base_offset);
306 307 308 309 310 311 312 313 314 315 316

	/* Mux clocking */
	/* Grant Support */
	ucc_set_qe_mux_grant(us_info->ucc_num, us_info->grant_support);
	/* Breakpoint Support */
	ucc_set_qe_mux_bkpt(us_info->ucc_num, us_info->brkpt_support);
	/* Set Tsa or NMSI mode. */
	ucc_set_qe_mux_tsa(us_info->ucc_num, us_info->tsa);
	/* If NMSI (not Tsa), set Tx and Rx clock. */
	if (!us_info->tsa) {
		/* Rx clock routing */
317 318
		if (ucc_set_qe_mux_rxtx(us_info->ucc_num, us_info->rx_clock,
					COMM_DIR_RX)) {
319
			printk(KERN_ERR "%s: illegal value for RX clock\n",
320
			       __FUNCTION__);
321 322 323 324
			ucc_slow_free(uccs);
			return -EINVAL;
		}
		/* Tx clock routing */
325 326
		if (ucc_set_qe_mux_rxtx(us_info->ucc_num, us_info->tx_clock,
					COMM_DIR_TX)) {
327
			printk(KERN_ERR "%s: illegal value for TX clock\n",
328
			       __FUNCTION__);
329 330 331 332 333 334 335 336
			ucc_slow_free(uccs);
			return -EINVAL;
		}
	}

	/* Set interrupt mask register at UCC level. */
	out_be16(&us_regs->uccm, us_info->uccm_mask);

337 338 339
	/* First, clear anything pending at UCC level,
	 * otherwise, old garbage may come through
	 * as soon as the dam is opened. */
340 341 342 343 344 345 346 347 348 349 350

	/* Writing '1' clears */
	out_be16(&us_regs->ucce, 0xffff);

	/* Issue QE Init command */
	if (us_info->init_tx && us_info->init_rx)
		command = QE_INIT_TX_RX;
	else if (us_info->init_tx)
		command = QE_INIT_TX;
	else
		command = QE_INIT_RX;	/* We know at least one is TRUE */
351 352

	qe_issue_cmd(command, id, us_info->protocol, 0);
353 354 355 356

	*uccs_ret = uccs;
	return 0;
}
357
EXPORT_SYMBOL(ucc_slow_init);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

void ucc_slow_free(struct ucc_slow_private * uccs)
{
	if (!uccs)
		return;

	if (uccs->rx_base_offset)
		qe_muram_free(uccs->rx_base_offset);

	if (uccs->tx_base_offset)
		qe_muram_free(uccs->tx_base_offset);

	if (uccs->us_pram) {
		qe_muram_free(uccs->us_pram_offset);
		uccs->us_pram = NULL;
	}

	kfree(uccs);
}
377
EXPORT_SYMBOL(ucc_slow_free);
378