linux/drivers/usb/gadget/r8a66597-udc.c
<<
>>
Prefs
   1/*
   2 * R8A66597 UDC (USB gadget)
   3 *
   4 * Copyright (C) 2006-2009 Renesas Solutions Corp.
   5 *
   6 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; version 2 of the License.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20 *
  21 */
  22
  23#include <linux/module.h>
  24#include <linux/interrupt.h>
  25#include <linux/delay.h>
  26#include <linux/io.h>
  27#include <linux/platform_device.h>
  28#include <linux/clk.h>
  29#include <linux/err.h>
  30#include <linux/slab.h>
  31
  32#include <linux/usb/ch9.h>
  33#include <linux/usb/gadget.h>
  34
  35#include "r8a66597-udc.h"
  36
  37#define DRIVER_VERSION  "2009-08-18"
  38
  39static const char udc_name[] = "r8a66597_udc";
  40static const char *r8a66597_ep_name[] = {
  41        "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7",
  42        "ep8", "ep9",
  43};
  44
  45static void init_controller(struct r8a66597 *r8a66597);
  46static void disable_controller(struct r8a66597 *r8a66597);
  47static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req);
  48static void irq_packet_write(struct r8a66597_ep *ep,
  49                                struct r8a66597_request *req);
  50static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
  51                        gfp_t gfp_flags);
  52
  53static void transfer_complete(struct r8a66597_ep *ep,
  54                struct r8a66597_request *req, int status);
  55
  56/*-------------------------------------------------------------------------*/
  57static inline u16 get_usb_speed(struct r8a66597 *r8a66597)
  58{
  59        return r8a66597_read(r8a66597, DVSTCTR0) & RHST;
  60}
  61
  62static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  63                unsigned long reg)
  64{
  65        u16 tmp;
  66
  67        tmp = r8a66597_read(r8a66597, INTENB0);
  68        r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
  69                        INTENB0);
  70        r8a66597_bset(r8a66597, (1 << pipenum), reg);
  71        r8a66597_write(r8a66597, tmp, INTENB0);
  72}
  73
  74static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  75                unsigned long reg)
  76{
  77        u16 tmp;
  78
  79        tmp = r8a66597_read(r8a66597, INTENB0);
  80        r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
  81                        INTENB0);
  82        r8a66597_bclr(r8a66597, (1 << pipenum), reg);
  83        r8a66597_write(r8a66597, tmp, INTENB0);
  84}
  85
  86static void r8a66597_usb_connect(struct r8a66597 *r8a66597)
  87{
  88        r8a66597_bset(r8a66597, CTRE, INTENB0);
  89        r8a66597_bset(r8a66597, BEMPE | BRDYE, INTENB0);
  90
  91        r8a66597_bset(r8a66597, DPRPU, SYSCFG0);
  92}
  93
  94static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597)
  95__releases(r8a66597->lock)
  96__acquires(r8a66597->lock)
  97{
  98        r8a66597_bclr(r8a66597, CTRE, INTENB0);
  99        r8a66597_bclr(r8a66597, BEMPE | BRDYE, INTENB0);
 100        r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
 101
 102        r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
 103        spin_unlock(&r8a66597->lock);
 104        r8a66597->driver->disconnect(&r8a66597->gadget);
 105        spin_lock(&r8a66597->lock);
 106
 107        disable_controller(r8a66597);
 108        init_controller(r8a66597);
 109        r8a66597_bset(r8a66597, VBSE, INTENB0);
 110        INIT_LIST_HEAD(&r8a66597->ep[0].queue);
 111}
 112
 113static inline u16 control_reg_get_pid(struct r8a66597 *r8a66597, u16 pipenum)
 114{
 115        u16 pid = 0;
 116        unsigned long offset;
 117
 118        if (pipenum == 0)
 119                pid = r8a66597_read(r8a66597, DCPCTR) & PID;
 120        else if (pipenum < R8A66597_MAX_NUM_PIPE) {
 121                offset = get_pipectr_addr(pipenum);
 122                pid = r8a66597_read(r8a66597, offset) & PID;
 123        } else
 124                printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
 125
 126        return pid;
 127}
 128
 129static inline void control_reg_set_pid(struct r8a66597 *r8a66597, u16 pipenum,
 130                u16 pid)
 131{
 132        unsigned long offset;
 133
 134        if (pipenum == 0)
 135                r8a66597_mdfy(r8a66597, pid, PID, DCPCTR);
 136        else if (pipenum < R8A66597_MAX_NUM_PIPE) {
 137                offset = get_pipectr_addr(pipenum);
 138                r8a66597_mdfy(r8a66597, pid, PID, offset);
 139        } else
 140                printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
 141}
 142
 143static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum)
 144{
 145        control_reg_set_pid(r8a66597, pipenum, PID_BUF);
 146}
 147
 148static inline void pipe_stop(struct r8a66597 *r8a66597, u16 pipenum)
 149{
 150        control_reg_set_pid(r8a66597, pipenum, PID_NAK);
 151}
 152
 153static inline void pipe_stall(struct r8a66597 *r8a66597, u16 pipenum)
 154{
 155        control_reg_set_pid(r8a66597, pipenum, PID_STALL);
 156}
 157
 158static inline u16 control_reg_get(struct r8a66597 *r8a66597, u16 pipenum)
 159{
 160        u16 ret = 0;
 161        unsigned long offset;
 162
 163        if (pipenum == 0)
 164                ret = r8a66597_read(r8a66597, DCPCTR);
 165        else if (pipenum < R8A66597_MAX_NUM_PIPE) {
 166                offset = get_pipectr_addr(pipenum);
 167                ret = r8a66597_read(r8a66597, offset);
 168        } else
 169                printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
 170
 171        return ret;
 172}
 173
 174static inline void control_reg_sqclr(struct r8a66597 *r8a66597, u16 pipenum)
 175{
 176        unsigned long offset;
 177
 178        pipe_stop(r8a66597, pipenum);
 179
 180        if (pipenum == 0)
 181                r8a66597_bset(r8a66597, SQCLR, DCPCTR);
 182        else if (pipenum < R8A66597_MAX_NUM_PIPE) {
 183                offset = get_pipectr_addr(pipenum);
 184                r8a66597_bset(r8a66597, SQCLR, offset);
 185        } else
 186                printk(KERN_ERR "unexpect pipe num(%d)\n", pipenum);
 187}
 188
 189static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum)
 190{
 191        u16 tmp;
 192        int size;
 193
 194        if (pipenum == 0) {
 195                tmp = r8a66597_read(r8a66597, DCPCFG);
 196                if ((tmp & R8A66597_CNTMD) != 0)
 197                        size = 256;
 198                else {
 199                        tmp = r8a66597_read(r8a66597, DCPMAXP);
 200                        size = tmp & MAXP;
 201                }
 202        } else {
 203                r8a66597_write(r8a66597, pipenum, PIPESEL);
 204                tmp = r8a66597_read(r8a66597, PIPECFG);
 205                if ((tmp & R8A66597_CNTMD) != 0) {
 206                        tmp = r8a66597_read(r8a66597, PIPEBUF);
 207                        size = ((tmp >> 10) + 1) * 64;
 208                } else {
 209                        tmp = r8a66597_read(r8a66597, PIPEMAXP);
 210                        size = tmp & MXPS;
 211                }
 212        }
 213
 214        return size;
 215}
 216
 217static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
 218{
 219        if (r8a66597->pdata->on_chip)
 220                return MBW_32;
 221        else
 222                return MBW_16;
 223}
 224
 225static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum)
 226{
 227        struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
 228
 229        if (ep->use_dma)
 230                return;
 231
 232        r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel);
 233
 234        ndelay(450);
 235
 236        r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
 237}
 238
 239static int pipe_buffer_setting(struct r8a66597 *r8a66597,
 240                struct r8a66597_pipe_info *info)
 241{
 242        u16 bufnum = 0, buf_bsize = 0;
 243        u16 pipecfg = 0;
 244
 245        if (info->pipe == 0)
 246                return -EINVAL;
 247
 248        r8a66597_write(r8a66597, info->pipe, PIPESEL);
 249
 250        if (info->dir_in)
 251                pipecfg |= R8A66597_DIR;
 252        pipecfg |= info->type;
 253        pipecfg |= info->epnum;
 254        switch (info->type) {
 255        case R8A66597_INT:
 256                bufnum = 4 + (info->pipe - R8A66597_BASE_PIPENUM_INT);
 257                buf_bsize = 0;
 258                break;
 259        case R8A66597_BULK:
 260                /* isochronous pipes may be used as bulk pipes */
 261                if (info->pipe >= R8A66597_BASE_PIPENUM_BULK)
 262                        bufnum = info->pipe - R8A66597_BASE_PIPENUM_BULK;
 263                else
 264                        bufnum = info->pipe - R8A66597_BASE_PIPENUM_ISOC;
 265
 266                bufnum = R8A66597_BASE_BUFNUM + (bufnum * 16);
 267                buf_bsize = 7;
 268                pipecfg |= R8A66597_DBLB;
 269                if (!info->dir_in)
 270                        pipecfg |= R8A66597_SHTNAK;
 271                break;
 272        case R8A66597_ISO:
 273                bufnum = R8A66597_BASE_BUFNUM +
 274                         (info->pipe - R8A66597_BASE_PIPENUM_ISOC) * 16;
 275                buf_bsize = 7;
 276                break;
 277        }
 278
 279        if (buf_bsize && ((bufnum + 16) >= R8A66597_MAX_BUFNUM)) {
 280                pr_err("r8a66597 pipe memory is insufficient\n");
 281                return -ENOMEM;
 282        }
 283
 284        r8a66597_write(r8a66597, pipecfg, PIPECFG);
 285        r8a66597_write(r8a66597, (buf_bsize << 10) | (bufnum), PIPEBUF);
 286        r8a66597_write(r8a66597, info->maxpacket, PIPEMAXP);
 287        if (info->interval)
 288                info->interval--;
 289        r8a66597_write(r8a66597, info->interval, PIPEPERI);
 290
 291        return 0;
 292}
 293
 294static void pipe_buffer_release(struct r8a66597 *r8a66597,
 295                                struct r8a66597_pipe_info *info)
 296{
 297        if (info->pipe == 0)
 298                return;
 299
 300        if (is_bulk_pipe(info->pipe))
 301                r8a66597->bulk--;
 302        else if (is_interrupt_pipe(info->pipe))
 303                r8a66597->interrupt--;
 304        else if (is_isoc_pipe(info->pipe)) {
 305                r8a66597->isochronous--;
 306                if (info->type == R8A66597_BULK)
 307                        r8a66597->bulk--;
 308        } else
 309                printk(KERN_ERR "ep_release: unexpect pipenum (%d)\n",
 310                                info->pipe);
 311}
 312
 313static void pipe_initialize(struct r8a66597_ep *ep)
 314{
 315        struct r8a66597 *r8a66597 = ep->r8a66597;
 316
 317        r8a66597_mdfy(r8a66597, 0, CURPIPE, ep->fifosel);
 318
 319        r8a66597_write(r8a66597, ACLRM, ep->pipectr);
 320        r8a66597_write(r8a66597, 0, ep->pipectr);
 321        r8a66597_write(r8a66597, SQCLR, ep->pipectr);
 322        if (ep->use_dma) {
 323                r8a66597_mdfy(r8a66597, ep->pipenum, CURPIPE, ep->fifosel);
 324
 325                ndelay(450);
 326
 327                r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
 328        }
 329}
 330
 331static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
 332                                struct r8a66597_ep *ep,
 333                                const struct usb_endpoint_descriptor *desc,
 334                                u16 pipenum, int dma)
 335{
 336        ep->use_dma = 0;
 337        ep->fifoaddr = CFIFO;
 338        ep->fifosel = CFIFOSEL;
 339        ep->fifoctr = CFIFOCTR;
 340        ep->fifotrn = 0;
 341
 342        ep->pipectr = get_pipectr_addr(pipenum);
 343        ep->pipenum = pipenum;
 344        ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
 345        r8a66597->pipenum2ep[pipenum] = ep;
 346        r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK]
 347                = ep;
 348        INIT_LIST_HEAD(&ep->queue);
 349}
 350
 351static void r8a66597_ep_release(struct r8a66597_ep *ep)
 352{
 353        struct r8a66597 *r8a66597 = ep->r8a66597;
 354        u16 pipenum = ep->pipenum;
 355
 356        if (pipenum == 0)
 357                return;
 358
 359        if (ep->use_dma)
 360                r8a66597->num_dma--;
 361        ep->pipenum = 0;
 362        ep->busy = 0;
 363        ep->use_dma = 0;
 364}
 365
 366static int alloc_pipe_config(struct r8a66597_ep *ep,
 367                const struct usb_endpoint_descriptor *desc)
 368{
 369        struct r8a66597 *r8a66597 = ep->r8a66597;
 370        struct r8a66597_pipe_info info;
 371        int dma = 0;
 372        unsigned char *counter;
 373        int ret;
 374
 375        ep->desc = desc;
 376
 377        if (ep->pipenum)        /* already allocated pipe  */
 378                return 0;
 379
 380        switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
 381        case USB_ENDPOINT_XFER_BULK:
 382                if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
 383                        if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
 384                                printk(KERN_ERR "bulk pipe is insufficient\n");
 385                                return -ENODEV;
 386                        } else {
 387                                info.pipe = R8A66597_BASE_PIPENUM_ISOC
 388                                                + r8a66597->isochronous;
 389                                counter = &r8a66597->isochronous;
 390                        }
 391                } else {
 392                        info.pipe = R8A66597_BASE_PIPENUM_BULK + r8a66597->bulk;
 393                        counter = &r8a66597->bulk;
 394                }
 395                info.type = R8A66597_BULK;
 396                dma = 1;
 397                break;
 398        case USB_ENDPOINT_XFER_INT:
 399                if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) {
 400                        printk(KERN_ERR "interrupt pipe is insufficient\n");
 401                        return -ENODEV;
 402                }
 403                info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt;
 404                info.type = R8A66597_INT;
 405                counter = &r8a66597->interrupt;
 406                break;
 407        case USB_ENDPOINT_XFER_ISOC:
 408                if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
 409                        printk(KERN_ERR "isochronous pipe is insufficient\n");
 410                        return -ENODEV;
 411                }
 412                info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous;
 413                info.type = R8A66597_ISO;
 414                counter = &r8a66597->isochronous;
 415                break;
 416        default:
 417                printk(KERN_ERR "unexpect xfer type\n");
 418                return -EINVAL;
 419        }
 420        ep->type = info.type;
 421
 422        info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
 423        info.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
 424        info.interval = desc->bInterval;
 425        if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
 426                info.dir_in = 1;
 427        else
 428                info.dir_in = 0;
 429
 430        ret = pipe_buffer_setting(r8a66597, &info);
 431        if (ret < 0) {
 432                printk(KERN_ERR "pipe_buffer_setting fail\n");
 433                return ret;
 434        }
 435
 436        (*counter)++;
 437        if ((counter == &r8a66597->isochronous) && info.type == R8A66597_BULK)
 438                r8a66597->bulk++;
 439
 440        r8a66597_ep_setting(r8a66597, ep, desc, info.pipe, dma);
 441        pipe_initialize(ep);
 442
 443        return 0;
 444}
 445
 446static int free_pipe_config(struct r8a66597_ep *ep)
 447{
 448        struct r8a66597 *r8a66597 = ep->r8a66597;
 449        struct r8a66597_pipe_info info;
 450
 451        info.pipe = ep->pipenum;
 452        info.type = ep->type;
 453        pipe_buffer_release(r8a66597, &info);
 454        r8a66597_ep_release(ep);
 455
 456        return 0;
 457}
 458
 459/*-------------------------------------------------------------------------*/
 460static void pipe_irq_enable(struct r8a66597 *r8a66597, u16 pipenum)
 461{
 462        enable_irq_ready(r8a66597, pipenum);
 463        enable_irq_nrdy(r8a66597, pipenum);
 464}
 465
 466static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
 467{
 468        disable_irq_ready(r8a66597, pipenum);
 469        disable_irq_nrdy(r8a66597, pipenum);
 470}
 471
 472/* if complete is true, gadget driver complete function is not call */
 473static void control_end(struct r8a66597 *r8a66597, unsigned ccpl)
 474{
 475        r8a66597->ep[0].internal_ccpl = ccpl;
 476        pipe_start(r8a66597, 0);
 477        r8a66597_bset(r8a66597, CCPL, DCPCTR);
 478}
 479
 480static void start_ep0_write(struct r8a66597_ep *ep,
 481                                struct r8a66597_request *req)
 482{
 483        struct r8a66597 *r8a66597 = ep->r8a66597;
 484
 485        pipe_change(r8a66597, ep->pipenum);
 486        r8a66597_mdfy(r8a66597, ISEL, (ISEL | CURPIPE), CFIFOSEL);
 487        r8a66597_write(r8a66597, BCLR, ep->fifoctr);
 488        if (req->req.length == 0) {
 489                r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
 490                pipe_start(r8a66597, 0);
 491                transfer_complete(ep, req, 0);
 492        } else {
 493                r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
 494                irq_ep0_write(ep, req);
 495        }
 496}
 497
 498static void start_packet_write(struct r8a66597_ep *ep,
 499                                struct r8a66597_request *req)
 500{
 501        struct r8a66597 *r8a66597 = ep->r8a66597;
 502        u16 tmp;
 503
 504        pipe_change(r8a66597, ep->pipenum);
 505        disable_irq_empty(r8a66597, ep->pipenum);
 506        pipe_start(r8a66597, ep->pipenum);
 507
 508        tmp = r8a66597_read(r8a66597, ep->fifoctr);
 509        if (unlikely((tmp & FRDY) == 0))
 510                pipe_irq_enable(r8a66597, ep->pipenum);
 511        else
 512                irq_packet_write(ep, req);
 513}
 514
 515static void start_packet_read(struct r8a66597_ep *ep,
 516                                struct r8a66597_request *req)
 517{
 518        struct r8a66597 *r8a66597 = ep->r8a66597;
 519        u16 pipenum = ep->pipenum;
 520
 521        if (ep->pipenum == 0) {
 522                r8a66597_mdfy(r8a66597, 0, (ISEL | CURPIPE), CFIFOSEL);
 523                r8a66597_write(r8a66597, BCLR, ep->fifoctr);
 524                pipe_start(r8a66597, pipenum);
 525                pipe_irq_enable(r8a66597, pipenum);
 526        } else {
 527                if (ep->use_dma) {
 528                        r8a66597_bset(r8a66597, TRCLR, ep->fifosel);
 529                        pipe_change(r8a66597, pipenum);
 530                        r8a66597_bset(r8a66597, TRENB, ep->fifosel);
 531                        r8a66597_write(r8a66597,
 532                                (req->req.length + ep->ep.maxpacket - 1)
 533                                        / ep->ep.maxpacket,
 534                                ep->fifotrn);
 535                }
 536                pipe_start(r8a66597, pipenum);  /* trigger once */
 537                pipe_irq_enable(r8a66597, pipenum);
 538        }
 539}
 540
 541static void start_packet(struct r8a66597_ep *ep, struct r8a66597_request *req)
 542{
 543        if (ep->desc->bEndpointAddress & USB_DIR_IN)
 544                start_packet_write(ep, req);
 545        else
 546                start_packet_read(ep, req);
 547}
 548
 549static void start_ep0(struct r8a66597_ep *ep, struct r8a66597_request *req)
 550{
 551        u16 ctsq;
 552
 553        ctsq = r8a66597_read(ep->r8a66597, INTSTS0) & CTSQ;
 554
 555        switch (ctsq) {
 556        case CS_RDDS:
 557                start_ep0_write(ep, req);
 558                break;
 559        case CS_WRDS:
 560                start_packet_read(ep, req);
 561                break;
 562
 563        case CS_WRND:
 564                control_end(ep->r8a66597, 0);
 565                break;
 566        default:
 567                printk(KERN_ERR "start_ep0: unexpect ctsq(%x)\n", ctsq);
 568                break;
 569        }
 570}
 571
 572static void init_controller(struct r8a66597 *r8a66597)
 573{
 574        u16 vif = r8a66597->pdata->vif ? LDRV : 0;
 575        u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
 576        u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
 577
 578        if (r8a66597->pdata->on_chip) {
 579                r8a66597_bset(r8a66597, 0x04, SYSCFG1);
 580                r8a66597_bset(r8a66597, HSE, SYSCFG0);
 581
 582                r8a66597_bclr(r8a66597, USBE, SYSCFG0);
 583                r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
 584                r8a66597_bset(r8a66597, USBE, SYSCFG0);
 585
 586                r8a66597_bset(r8a66597, SCKE, SYSCFG0);
 587
 588                r8a66597_bset(r8a66597, irq_sense, INTENB1);
 589                r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
 590                                DMA0CFG);
 591        } else {
 592                r8a66597_bset(r8a66597, vif | endian, PINCFG);
 593                r8a66597_bset(r8a66597, HSE, SYSCFG0);          /* High spd */
 594                r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
 595                                XTAL, SYSCFG0);
 596
 597                r8a66597_bclr(r8a66597, USBE, SYSCFG0);
 598                r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
 599                r8a66597_bset(r8a66597, USBE, SYSCFG0);
 600
 601                r8a66597_bset(r8a66597, XCKE, SYSCFG0);
 602
 603                msleep(3);
 604
 605                r8a66597_bset(r8a66597, PLLC, SYSCFG0);
 606
 607                msleep(1);
 608
 609                r8a66597_bset(r8a66597, SCKE, SYSCFG0);
 610
 611                r8a66597_bset(r8a66597, irq_sense, INTENB1);
 612                r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
 613                               DMA0CFG);
 614        }
 615}
 616
 617static void disable_controller(struct r8a66597 *r8a66597)
 618{
 619        if (r8a66597->pdata->on_chip) {
 620                r8a66597_bset(r8a66597, SCKE, SYSCFG0);
 621
 622                /* disable interrupts */
 623                r8a66597_write(r8a66597, 0, INTENB0);
 624                r8a66597_write(r8a66597, 0, INTENB1);
 625                r8a66597_write(r8a66597, 0, BRDYENB);
 626                r8a66597_write(r8a66597, 0, BEMPENB);
 627                r8a66597_write(r8a66597, 0, NRDYENB);
 628
 629                /* clear status */
 630                r8a66597_write(r8a66597, 0, BRDYSTS);
 631                r8a66597_write(r8a66597, 0, NRDYSTS);
 632                r8a66597_write(r8a66597, 0, BEMPSTS);
 633
 634                r8a66597_bclr(r8a66597, USBE, SYSCFG0);
 635                r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
 636
 637        } else {
 638                r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
 639                udelay(1);
 640                r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
 641                udelay(1);
 642                udelay(1);
 643                r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
 644        }
 645}
 646
 647static void r8a66597_start_xclock(struct r8a66597 *r8a66597)
 648{
 649        u16 tmp;
 650
 651        if (!r8a66597->pdata->on_chip) {
 652                tmp = r8a66597_read(r8a66597, SYSCFG0);
 653                if (!(tmp & XCKE))
 654                        r8a66597_bset(r8a66597, XCKE, SYSCFG0);
 655        }
 656}
 657
 658static struct r8a66597_request *get_request_from_ep(struct r8a66597_ep *ep)
 659{
 660        return list_entry(ep->queue.next, struct r8a66597_request, queue);
 661}
 662
 663/*-------------------------------------------------------------------------*/
 664static void transfer_complete(struct r8a66597_ep *ep,
 665                struct r8a66597_request *req, int status)
 666__releases(r8a66597->lock)
 667__acquires(r8a66597->lock)
 668{
 669        int restart = 0;
 670
 671        if (unlikely(ep->pipenum == 0)) {
 672                if (ep->internal_ccpl) {
 673                        ep->internal_ccpl = 0;
 674                        return;
 675                }
 676        }
 677
 678        list_del_init(&req->queue);
 679        if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
 680                req->req.status = -ESHUTDOWN;
 681        else
 682                req->req.status = status;
 683
 684        if (!list_empty(&ep->queue))
 685                restart = 1;
 686
 687        spin_unlock(&ep->r8a66597->lock);
 688        req->req.complete(&ep->ep, &req->req);
 689        spin_lock(&ep->r8a66597->lock);
 690
 691        if (restart) {
 692                req = get_request_from_ep(ep);
 693                if (ep->desc)
 694                        start_packet(ep, req);
 695        }
 696}
 697
 698static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
 699{
 700        int i;
 701        u16 tmp;
 702        unsigned bufsize;
 703        size_t size;
 704        void *buf;
 705        u16 pipenum = ep->pipenum;
 706        struct r8a66597 *r8a66597 = ep->r8a66597;
 707
 708        pipe_change(r8a66597, pipenum);
 709        r8a66597_bset(r8a66597, ISEL, ep->fifosel);
 710
 711        i = 0;
 712        do {
 713                tmp = r8a66597_read(r8a66597, ep->fifoctr);
 714                if (i++ > 100000) {
 715                        printk(KERN_ERR "pipe0 is busy. maybe cpu i/o bus"
 716                                "conflict. please power off this controller.");
 717                        return;
 718                }
 719                ndelay(1);
 720        } while ((tmp & FRDY) == 0);
 721
 722        /* prepare parameters */
 723        bufsize = get_buffer_size(r8a66597, pipenum);
 724        buf = req->req.buf + req->req.actual;
 725        size = min(bufsize, req->req.length - req->req.actual);
 726
 727        /* write fifo */
 728        if (req->req.buf) {
 729                if (size > 0)
 730                        r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
 731                if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
 732                        r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
 733        }
 734
 735        /* update parameters */
 736        req->req.actual += size;
 737
 738        /* check transfer finish */
 739        if ((!req->req.zero && (req->req.actual == req->req.length))
 740                        || (size % ep->ep.maxpacket)
 741                        || (size == 0)) {
 742                disable_irq_ready(r8a66597, pipenum);
 743                disable_irq_empty(r8a66597, pipenum);
 744        } else {
 745                disable_irq_ready(r8a66597, pipenum);
 746                enable_irq_empty(r8a66597, pipenum);
 747        }
 748        pipe_start(r8a66597, pipenum);
 749}
 750
 751static void irq_packet_write(struct r8a66597_ep *ep,
 752                                struct r8a66597_request *req)
 753{
 754        u16 tmp;
 755        unsigned bufsize;
 756        size_t size;
 757        void *buf;
 758        u16 pipenum = ep->pipenum;
 759        struct r8a66597 *r8a66597 = ep->r8a66597;
 760
 761        pipe_change(r8a66597, pipenum);
 762        tmp = r8a66597_read(r8a66597, ep->fifoctr);
 763        if (unlikely((tmp & FRDY) == 0)) {
 764                pipe_stop(r8a66597, pipenum);
 765                pipe_irq_disable(r8a66597, pipenum);
 766                printk(KERN_ERR "write fifo not ready. pipnum=%d\n", pipenum);
 767                return;
 768        }
 769
 770        /* prepare parameters */
 771        bufsize = get_buffer_size(r8a66597, pipenum);
 772        buf = req->req.buf + req->req.actual;
 773        size = min(bufsize, req->req.length - req->req.actual);
 774
 775        /* write fifo */
 776        if (req->req.buf) {
 777                r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
 778                if ((size == 0)
 779                                || ((size % ep->ep.maxpacket) != 0)
 780                                || ((bufsize != ep->ep.maxpacket)
 781                                        && (bufsize > size)))
 782                        r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
 783        }
 784
 785        /* update parameters */
 786        req->req.actual += size;
 787        /* check transfer finish */
 788        if ((!req->req.zero && (req->req.actual == req->req.length))
 789                        || (size % ep->ep.maxpacket)
 790                        || (size == 0)) {
 791                disable_irq_ready(r8a66597, pipenum);
 792                enable_irq_empty(r8a66597, pipenum);
 793        } else {
 794                disable_irq_empty(r8a66597, pipenum);
 795                pipe_irq_enable(r8a66597, pipenum);
 796        }
 797}
 798
 799static void irq_packet_read(struct r8a66597_ep *ep,
 800                                struct r8a66597_request *req)
 801{
 802        u16 tmp;
 803        int rcv_len, bufsize, req_len;
 804        int size;
 805        void *buf;
 806        u16 pipenum = ep->pipenum;
 807        struct r8a66597 *r8a66597 = ep->r8a66597;
 808        int finish = 0;
 809
 810        pipe_change(r8a66597, pipenum);
 811        tmp = r8a66597_read(r8a66597, ep->fifoctr);
 812        if (unlikely((tmp & FRDY) == 0)) {
 813                req->req.status = -EPIPE;
 814                pipe_stop(r8a66597, pipenum);
 815                pipe_irq_disable(r8a66597, pipenum);
 816                printk(KERN_ERR "read fifo not ready");
 817                return;
 818        }
 819
 820        /* prepare parameters */
 821        rcv_len = tmp & DTLN;
 822        bufsize = get_buffer_size(r8a66597, pipenum);
 823
 824        buf = req->req.buf + req->req.actual;
 825        req_len = req->req.length - req->req.actual;
 826        if (rcv_len < bufsize)
 827                size = min(rcv_len, req_len);
 828        else
 829                size = min(bufsize, req_len);
 830
 831        /* update parameters */
 832        req->req.actual += size;
 833
 834        /* check transfer finish */
 835        if ((!req->req.zero && (req->req.actual == req->req.length))
 836                        || (size % ep->ep.maxpacket)
 837                        || (size == 0)) {
 838                pipe_stop(r8a66597, pipenum);
 839                pipe_irq_disable(r8a66597, pipenum);
 840                finish = 1;
 841        }
 842
 843        /* read fifo */
 844        if (req->req.buf) {
 845                if (size == 0)
 846                        r8a66597_write(r8a66597, BCLR, ep->fifoctr);
 847                else
 848                        r8a66597_read_fifo(r8a66597, ep->fifoaddr, buf, size);
 849
 850        }
 851
 852        if ((ep->pipenum != 0) && finish)
 853                transfer_complete(ep, req, 0);
 854}
 855
 856static void irq_pipe_ready(struct r8a66597 *r8a66597, u16 status, u16 enb)
 857{
 858        u16 check;
 859        u16 pipenum;
 860        struct r8a66597_ep *ep;
 861        struct r8a66597_request *req;
 862
 863        if ((status & BRDY0) && (enb & BRDY0)) {
 864                r8a66597_write(r8a66597, ~BRDY0, BRDYSTS);
 865                r8a66597_mdfy(r8a66597, 0, CURPIPE, CFIFOSEL);
 866
 867                ep = &r8a66597->ep[0];
 868                req = get_request_from_ep(ep);
 869                irq_packet_read(ep, req);
 870        } else {
 871                for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
 872                        check = 1 << pipenum;
 873                        if ((status & check) && (enb & check)) {
 874                                r8a66597_write(r8a66597, ~check, BRDYSTS);
 875                                ep = r8a66597->pipenum2ep[pipenum];
 876                                req = get_request_from_ep(ep);
 877                                if (ep->desc->bEndpointAddress & USB_DIR_IN)
 878                                        irq_packet_write(ep, req);
 879                                else
 880                                        irq_packet_read(ep, req);
 881                        }
 882                }
 883        }
 884}
 885
 886static void irq_pipe_empty(struct r8a66597 *r8a66597, u16 status, u16 enb)
 887{
 888        u16 tmp;
 889        u16 check;
 890        u16 pipenum;
 891        struct r8a66597_ep *ep;
 892        struct r8a66597_request *req;
 893
 894        if ((status & BEMP0) && (enb & BEMP0)) {
 895                r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
 896
 897                ep = &r8a66597->ep[0];
 898                req = get_request_from_ep(ep);
 899                irq_ep0_write(ep, req);
 900        } else {
 901                for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
 902                        check = 1 << pipenum;
 903                        if ((status & check) && (enb & check)) {
 904                                r8a66597_write(r8a66597, ~check, BEMPSTS);
 905                                tmp = control_reg_get(r8a66597, pipenum);
 906                                if ((tmp & INBUFM) == 0) {
 907                                        disable_irq_empty(r8a66597, pipenum);
 908                                        pipe_irq_disable(r8a66597, pipenum);
 909                                        pipe_stop(r8a66597, pipenum);
 910                                        ep = r8a66597->pipenum2ep[pipenum];
 911                                        req = get_request_from_ep(ep);
 912                                        if (!list_empty(&ep->queue))
 913                                                transfer_complete(ep, req, 0);
 914                                }
 915                        }
 916                }
 917        }
 918}
 919
 920static void get_status(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
 921__releases(r8a66597->lock)
 922__acquires(r8a66597->lock)
 923{
 924        struct r8a66597_ep *ep;
 925        u16 pid;
 926        u16 status = 0;
 927        u16 w_index = le16_to_cpu(ctrl->wIndex);
 928
 929        switch (ctrl->bRequestType & USB_RECIP_MASK) {
 930        case USB_RECIP_DEVICE:
 931                status = 1 << USB_DEVICE_SELF_POWERED;
 932                break;
 933        case USB_RECIP_INTERFACE:
 934                status = 0;
 935                break;
 936        case USB_RECIP_ENDPOINT:
 937                ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
 938                pid = control_reg_get_pid(r8a66597, ep->pipenum);
 939                if (pid == PID_STALL)
 940                        status = 1 << USB_ENDPOINT_HALT;
 941                else
 942                        status = 0;
 943                break;
 944        default:
 945                pipe_stall(r8a66597, 0);
 946                return;         /* exit */
 947        }
 948
 949        r8a66597->ep0_data = cpu_to_le16(status);
 950        r8a66597->ep0_req->buf = &r8a66597->ep0_data;
 951        r8a66597->ep0_req->length = 2;
 952        /* AV: what happens if we get called again before that gets through? */
 953        spin_unlock(&r8a66597->lock);
 954        r8a66597_queue(r8a66597->gadget.ep0, r8a66597->ep0_req, GFP_KERNEL);
 955        spin_lock(&r8a66597->lock);
 956}
 957
 958static void clear_feature(struct r8a66597 *r8a66597,
 959                                struct usb_ctrlrequest *ctrl)
 960{
 961        switch (ctrl->bRequestType & USB_RECIP_MASK) {
 962        case USB_RECIP_DEVICE:
 963                control_end(r8a66597, 1);
 964                break;
 965        case USB_RECIP_INTERFACE:
 966                control_end(r8a66597, 1);
 967                break;
 968        case USB_RECIP_ENDPOINT: {
 969                struct r8a66597_ep *ep;
 970                struct r8a66597_request *req;
 971                u16 w_index = le16_to_cpu(ctrl->wIndex);
 972
 973                ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
 974                if (!ep->wedge) {
 975                        pipe_stop(r8a66597, ep->pipenum);
 976                        control_reg_sqclr(r8a66597, ep->pipenum);
 977                        spin_unlock(&r8a66597->lock);
 978                        usb_ep_clear_halt(&ep->ep);
 979                        spin_lock(&r8a66597->lock);
 980                }
 981
 982                control_end(r8a66597, 1);
 983
 984                req = get_request_from_ep(ep);
 985                if (ep->busy) {
 986                        ep->busy = 0;
 987                        if (list_empty(&ep->queue))
 988                                break;
 989                        start_packet(ep, req);
 990                } else if (!list_empty(&ep->queue))
 991                        pipe_start(r8a66597, ep->pipenum);
 992                }
 993                break;
 994        default:
 995                pipe_stall(r8a66597, 0);
 996                break;
 997        }
 998}
 999
1000static void set_feature(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1001{
1002
1003        switch (ctrl->bRequestType & USB_RECIP_MASK) {
1004        case USB_RECIP_DEVICE:
1005                control_end(r8a66597, 1);
1006                break;
1007        case USB_RECIP_INTERFACE:
1008                control_end(r8a66597, 1);
1009                break;
1010        case USB_RECIP_ENDPOINT: {
1011                struct r8a66597_ep *ep;
1012                u16 w_index = le16_to_cpu(ctrl->wIndex);
1013
1014                ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
1015                pipe_stall(r8a66597, ep->pipenum);
1016
1017                control_end(r8a66597, 1);
1018                }
1019                break;
1020        default:
1021                pipe_stall(r8a66597, 0);
1022                break;
1023        }
1024}
1025
1026/* if return value is true, call class driver's setup() */
1027static int setup_packet(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1028{
1029        u16 *p = (u16 *)ctrl;
1030        unsigned long offset = USBREQ;
1031        int i, ret = 0;
1032
1033        /* read fifo */
1034        r8a66597_write(r8a66597, ~VALID, INTSTS0);
1035
1036        for (i = 0; i < 4; i++)
1037                p[i] = r8a66597_read(r8a66597, offset + i*2);
1038
1039        /* check request */
1040        if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1041                switch (ctrl->bRequest) {
1042                case USB_REQ_GET_STATUS:
1043                        get_status(r8a66597, ctrl);
1044                        break;
1045                case USB_REQ_CLEAR_FEATURE:
1046                        clear_feature(r8a66597, ctrl);
1047                        break;
1048                case USB_REQ_SET_FEATURE:
1049                        set_feature(r8a66597, ctrl);
1050                        break;
1051                default:
1052                        ret = 1;
1053                        break;
1054                }
1055        } else
1056                ret = 1;
1057        return ret;
1058}
1059
1060static void r8a66597_update_usb_speed(struct r8a66597 *r8a66597)
1061{
1062        u16 speed = get_usb_speed(r8a66597);
1063
1064        switch (speed) {
1065        case HSMODE:
1066                r8a66597->gadget.speed = USB_SPEED_HIGH;
1067                break;
1068        case FSMODE:
1069                r8a66597->gadget.speed = USB_SPEED_FULL;
1070                break;
1071        default:
1072                r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
1073                printk(KERN_ERR "USB speed unknown\n");
1074        }
1075}
1076
1077static void irq_device_state(struct r8a66597 *r8a66597)
1078{
1079        u16 dvsq;
1080
1081        dvsq = r8a66597_read(r8a66597, INTSTS0) & DVSQ;
1082        r8a66597_write(r8a66597, ~DVST, INTSTS0);
1083
1084        if (dvsq == DS_DFLT) {
1085                /* bus reset */
1086                r8a66597->driver->disconnect(&r8a66597->gadget);
1087                r8a66597_update_usb_speed(r8a66597);
1088        }
1089        if (r8a66597->old_dvsq == DS_CNFG && dvsq != DS_CNFG)
1090                r8a66597_update_usb_speed(r8a66597);
1091        if ((dvsq == DS_CNFG || dvsq == DS_ADDS)
1092                        && r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1093                r8a66597_update_usb_speed(r8a66597);
1094
1095        r8a66597->old_dvsq = dvsq;
1096}
1097
1098static void irq_control_stage(struct r8a66597 *r8a66597)
1099__releases(r8a66597->lock)
1100__acquires(r8a66597->lock)
1101{
1102        struct usb_ctrlrequest ctrl;
1103        u16 ctsq;
1104
1105        ctsq = r8a66597_read(r8a66597, INTSTS0) & CTSQ;
1106        r8a66597_write(r8a66597, ~CTRT, INTSTS0);
1107
1108        switch (ctsq) {
1109        case CS_IDST: {
1110                struct r8a66597_ep *ep;
1111                struct r8a66597_request *req;
1112                ep = &r8a66597->ep[0];
1113                req = get_request_from_ep(ep);
1114                transfer_complete(ep, req, 0);
1115                }
1116                break;
1117
1118        case CS_RDDS:
1119        case CS_WRDS:
1120        case CS_WRND:
1121                if (setup_packet(r8a66597, &ctrl)) {
1122                        spin_unlock(&r8a66597->lock);
1123                        if (r8a66597->driver->setup(&r8a66597->gadget, &ctrl)
1124                                < 0)
1125                                pipe_stall(r8a66597, 0);
1126                        spin_lock(&r8a66597->lock);
1127                }
1128                break;
1129        case CS_RDSS:
1130        case CS_WRSS:
1131                control_end(r8a66597, 0);
1132                break;
1133        default:
1134                printk(KERN_ERR "ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1135                break;
1136        }
1137}
1138
1139static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1140{
1141        struct r8a66597 *r8a66597 = _r8a66597;
1142        u16 intsts0;
1143        u16 intenb0;
1144        u16 brdysts, nrdysts, bempsts;
1145        u16 brdyenb, nrdyenb, bempenb;
1146        u16 savepipe;
1147        u16 mask0;
1148
1149        spin_lock(&r8a66597->lock);
1150
1151        intsts0 = r8a66597_read(r8a66597, INTSTS0);
1152        intenb0 = r8a66597_read(r8a66597, INTENB0);
1153
1154        savepipe = r8a66597_read(r8a66597, CFIFOSEL);
1155
1156        mask0 = intsts0 & intenb0;
1157        if (mask0) {
1158                brdysts = r8a66597_read(r8a66597, BRDYSTS);
1159                nrdysts = r8a66597_read(r8a66597, NRDYSTS);
1160                bempsts = r8a66597_read(r8a66597, BEMPSTS);
1161                brdyenb = r8a66597_read(r8a66597, BRDYENB);
1162                nrdyenb = r8a66597_read(r8a66597, NRDYENB);
1163                bempenb = r8a66597_read(r8a66597, BEMPENB);
1164
1165                if (mask0 & VBINT) {
1166                        r8a66597_write(r8a66597,  0xffff & ~VBINT,
1167                                        INTSTS0);
1168                        r8a66597_start_xclock(r8a66597);
1169
1170                        /* start vbus sampling */
1171                        r8a66597->old_vbus = r8a66597_read(r8a66597, INTSTS0)
1172                                        & VBSTS;
1173                        r8a66597->scount = R8A66597_MAX_SAMPLING;
1174
1175                        mod_timer(&r8a66597->timer,
1176                                        jiffies + msecs_to_jiffies(50));
1177                }
1178                if (intsts0 & DVSQ)
1179                        irq_device_state(r8a66597);
1180
1181                if ((intsts0 & BRDY) && (intenb0 & BRDYE)
1182                                && (brdysts & brdyenb))
1183                        irq_pipe_ready(r8a66597, brdysts, brdyenb);
1184                if ((intsts0 & BEMP) && (intenb0 & BEMPE)
1185                                && (bempsts & bempenb))
1186                        irq_pipe_empty(r8a66597, bempsts, bempenb);
1187
1188                if (intsts0 & CTRT)
1189                        irq_control_stage(r8a66597);
1190        }
1191
1192        r8a66597_write(r8a66597, savepipe, CFIFOSEL);
1193
1194        spin_unlock(&r8a66597->lock);
1195        return IRQ_HANDLED;
1196}
1197
1198static void r8a66597_timer(unsigned long _r8a66597)
1199{
1200        struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1201        unsigned long flags;
1202        u16 tmp;
1203
1204        spin_lock_irqsave(&r8a66597->lock, flags);
1205        tmp = r8a66597_read(r8a66597, SYSCFG0);
1206        if (r8a66597->scount > 0) {
1207                tmp = r8a66597_read(r8a66597, INTSTS0) & VBSTS;
1208                if (tmp == r8a66597->old_vbus) {
1209                        r8a66597->scount--;
1210                        if (r8a66597->scount == 0) {
1211                                if (tmp == VBSTS)
1212                                        r8a66597_usb_connect(r8a66597);
1213                                else
1214                                        r8a66597_usb_disconnect(r8a66597);
1215                        } else {
1216                                mod_timer(&r8a66597->timer,
1217                                        jiffies + msecs_to_jiffies(50));
1218                        }
1219                } else {
1220                        r8a66597->scount = R8A66597_MAX_SAMPLING;
1221                        r8a66597->old_vbus = tmp;
1222                        mod_timer(&r8a66597->timer,
1223                                        jiffies + msecs_to_jiffies(50));
1224                }
1225        }
1226        spin_unlock_irqrestore(&r8a66597->lock, flags);
1227}
1228
1229/*-------------------------------------------------------------------------*/
1230static int r8a66597_enable(struct usb_ep *_ep,
1231                         const struct usb_endpoint_descriptor *desc)
1232{
1233        struct r8a66597_ep *ep;
1234
1235        ep = container_of(_ep, struct r8a66597_ep, ep);
1236        return alloc_pipe_config(ep, desc);
1237}
1238
1239static int r8a66597_disable(struct usb_ep *_ep)
1240{
1241        struct r8a66597_ep *ep;
1242        struct r8a66597_request *req;
1243        unsigned long flags;
1244
1245        ep = container_of(_ep, struct r8a66597_ep, ep);
1246        BUG_ON(!ep);
1247
1248        while (!list_empty(&ep->queue)) {
1249                req = get_request_from_ep(ep);
1250                spin_lock_irqsave(&ep->r8a66597->lock, flags);
1251                transfer_complete(ep, req, -ECONNRESET);
1252                spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1253        }
1254
1255        pipe_irq_disable(ep->r8a66597, ep->pipenum);
1256        return free_pipe_config(ep);
1257}
1258
1259static struct usb_request *r8a66597_alloc_request(struct usb_ep *_ep,
1260                                                gfp_t gfp_flags)
1261{
1262        struct r8a66597_request *req;
1263
1264        req = kzalloc(sizeof(struct r8a66597_request), gfp_flags);
1265        if (!req)
1266                return NULL;
1267
1268        INIT_LIST_HEAD(&req->queue);
1269
1270        return &req->req;
1271}
1272
1273static void r8a66597_free_request(struct usb_ep *_ep, struct usb_request *_req)
1274{
1275        struct r8a66597_request *req;
1276
1277        req = container_of(_req, struct r8a66597_request, req);
1278        kfree(req);
1279}
1280
1281static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
1282                        gfp_t gfp_flags)
1283{
1284        struct r8a66597_ep *ep;
1285        struct r8a66597_request *req;
1286        unsigned long flags;
1287        int request = 0;
1288
1289        ep = container_of(_ep, struct r8a66597_ep, ep);
1290        req = container_of(_req, struct r8a66597_request, req);
1291
1292        if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1293                return -ESHUTDOWN;
1294
1295        spin_lock_irqsave(&ep->r8a66597->lock, flags);
1296
1297        if (list_empty(&ep->queue))
1298                request = 1;
1299
1300        list_add_tail(&req->queue, &ep->queue);
1301        req->req.actual = 0;
1302        req->req.status = -EINPROGRESS;
1303
1304        if (ep->desc == NULL)   /* control */
1305                start_ep0(ep, req);
1306        else {
1307                if (request && !ep->busy)
1308                        start_packet(ep, req);
1309        }
1310
1311        spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1312
1313        return 0;
1314}
1315
1316static int r8a66597_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1317{
1318        struct r8a66597_ep *ep;
1319        struct r8a66597_request *req;
1320        unsigned long flags;
1321
1322        ep = container_of(_ep, struct r8a66597_ep, ep);
1323        req = container_of(_req, struct r8a66597_request, req);
1324
1325        spin_lock_irqsave(&ep->r8a66597->lock, flags);
1326        if (!list_empty(&ep->queue))
1327                transfer_complete(ep, req, -ECONNRESET);
1328        spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1329
1330        return 0;
1331}
1332
1333static int r8a66597_set_halt(struct usb_ep *_ep, int value)
1334{
1335        struct r8a66597_ep *ep;
1336        struct r8a66597_request *req;
1337        unsigned long flags;
1338        int ret = 0;
1339
1340        ep = container_of(_ep, struct r8a66597_ep, ep);
1341        req = get_request_from_ep(ep);
1342
1343        spin_lock_irqsave(&ep->r8a66597->lock, flags);
1344        if (!list_empty(&ep->queue)) {
1345                ret = -EAGAIN;
1346                goto out;
1347        }
1348        if (value) {
1349                ep->busy = 1;
1350                pipe_stall(ep->r8a66597, ep->pipenum);
1351        } else {
1352                ep->busy = 0;
1353                ep->wedge = 0;
1354                pipe_stop(ep->r8a66597, ep->pipenum);
1355        }
1356
1357out:
1358        spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1359        return ret;
1360}
1361
1362static int r8a66597_set_wedge(struct usb_ep *_ep)
1363{
1364        struct r8a66597_ep *ep;
1365        unsigned long flags;
1366
1367        ep = container_of(_ep, struct r8a66597_ep, ep);
1368
1369        if (!ep || !ep->desc)
1370                return -EINVAL;
1371
1372        spin_lock_irqsave(&ep->r8a66597->lock, flags);
1373        ep->wedge = 1;
1374        spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1375
1376        return usb_ep_set_halt(_ep);
1377}
1378
1379static void r8a66597_fifo_flush(struct usb_ep *_ep)
1380{
1381        struct r8a66597_ep *ep;
1382        unsigned long flags;
1383
1384        ep = container_of(_ep, struct r8a66597_ep, ep);
1385        spin_lock_irqsave(&ep->r8a66597->lock, flags);
1386        if (list_empty(&ep->queue) && !ep->busy) {
1387                pipe_stop(ep->r8a66597, ep->pipenum);
1388                r8a66597_bclr(ep->r8a66597, BCLR, ep->fifoctr);
1389        }
1390        spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1391}
1392
1393static struct usb_ep_ops r8a66597_ep_ops = {
1394        .enable         = r8a66597_enable,
1395        .disable        = r8a66597_disable,
1396
1397        .alloc_request  = r8a66597_alloc_request,
1398        .free_request   = r8a66597_free_request,
1399
1400        .queue          = r8a66597_queue,
1401        .dequeue        = r8a66597_dequeue,
1402
1403        .set_halt       = r8a66597_set_halt,
1404        .set_wedge      = r8a66597_set_wedge,
1405        .fifo_flush     = r8a66597_fifo_flush,
1406};
1407
1408/*-------------------------------------------------------------------------*/
1409static struct r8a66597 *the_controller;
1410
1411int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1412                int (*bind)(struct usb_gadget *))
1413{
1414        struct r8a66597 *r8a66597 = the_controller;
1415        int retval;
1416
1417        if (!driver
1418                        || driver->speed != USB_SPEED_HIGH
1419                        || !bind
1420                        || !driver->setup)
1421                return -EINVAL;
1422        if (!r8a66597)
1423                return -ENODEV;
1424        if (r8a66597->driver)
1425                return -EBUSY;
1426
1427        /* hook up the driver */
1428        driver->driver.bus = NULL;
1429        r8a66597->driver = driver;
1430        r8a66597->gadget.dev.driver = &driver->driver;
1431
1432        retval = device_add(&r8a66597->gadget.dev);
1433        if (retval) {
1434                printk(KERN_ERR "device_add error (%d)\n", retval);
1435                goto error;
1436        }
1437
1438        retval = bind(&r8a66597->gadget);
1439        if (retval) {
1440                printk(KERN_ERR "bind to driver error (%d)\n", retval);
1441                device_del(&r8a66597->gadget.dev);
1442                goto error;
1443        }
1444
1445        r8a66597_bset(r8a66597, VBSE, INTENB0);
1446        if (r8a66597_read(r8a66597, INTSTS0) & VBSTS) {
1447                r8a66597_start_xclock(r8a66597);
1448                /* start vbus sampling */
1449                r8a66597->old_vbus = r8a66597_read(r8a66597,
1450                                         INTSTS0) & VBSTS;
1451                r8a66597->scount = R8A66597_MAX_SAMPLING;
1452                mod_timer(&r8a66597->timer, jiffies + msecs_to_jiffies(50));
1453        }
1454
1455        return 0;
1456
1457error:
1458        r8a66597->driver = NULL;
1459        r8a66597->gadget.dev.driver = NULL;
1460
1461        return retval;
1462}
1463EXPORT_SYMBOL(usb_gadget_probe_driver);
1464
1465int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1466{
1467        struct r8a66597 *r8a66597 = the_controller;
1468        unsigned long flags;
1469
1470        if (driver != r8a66597->driver || !driver->unbind)
1471                return -EINVAL;
1472
1473        spin_lock_irqsave(&r8a66597->lock, flags);
1474        if (r8a66597->gadget.speed != USB_SPEED_UNKNOWN)
1475                r8a66597_usb_disconnect(r8a66597);
1476        spin_unlock_irqrestore(&r8a66597->lock, flags);
1477
1478        r8a66597_bclr(r8a66597, VBSE, INTENB0);
1479
1480        driver->unbind(&r8a66597->gadget);
1481
1482        init_controller(r8a66597);
1483        disable_controller(r8a66597);
1484
1485        device_del(&r8a66597->gadget.dev);
1486        r8a66597->driver = NULL;
1487        return 0;
1488}
1489EXPORT_SYMBOL(usb_gadget_unregister_driver);
1490
1491/*-------------------------------------------------------------------------*/
1492static int r8a66597_get_frame(struct usb_gadget *_gadget)
1493{
1494        struct r8a66597 *r8a66597 = gadget_to_r8a66597(_gadget);
1495        return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1496}
1497
1498static struct usb_gadget_ops r8a66597_gadget_ops = {
1499        .get_frame              = r8a66597_get_frame,
1500};
1501
1502static int __exit r8a66597_remove(struct platform_device *pdev)
1503{
1504        struct r8a66597         *r8a66597 = dev_get_drvdata(&pdev->dev);
1505
1506        del_timer_sync(&r8a66597->timer);
1507        iounmap(r8a66597->reg);
1508        free_irq(platform_get_irq(pdev, 0), r8a66597);
1509        r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
1510#ifdef CONFIG_HAVE_CLK
1511        if (r8a66597->pdata->on_chip) {
1512                clk_disable(r8a66597->clk);
1513                clk_put(r8a66597->clk);
1514        }
1515#endif
1516        kfree(r8a66597);
1517        return 0;
1518}
1519
1520static void nop_completion(struct usb_ep *ep, struct usb_request *r)
1521{
1522}
1523
1524static int __init r8a66597_probe(struct platform_device *pdev)
1525{
1526#ifdef CONFIG_HAVE_CLK
1527        char clk_name[8];
1528#endif
1529        struct resource *res, *ires;
1530        int irq;
1531        void __iomem *reg = NULL;
1532        struct r8a66597 *r8a66597 = NULL;
1533        int ret = 0;
1534        int i;
1535        unsigned long irq_trigger;
1536
1537        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1538        if (!res) {
1539                ret = -ENODEV;
1540                printk(KERN_ERR "platform_get_resource error.\n");
1541                goto clean_up;
1542        }
1543
1544        ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1545        irq = ires->start;
1546        irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
1547
1548        if (irq < 0) {
1549                ret = -ENODEV;
1550                printk(KERN_ERR "platform_get_irq error.\n");
1551                goto clean_up;
1552        }
1553
1554        reg = ioremap(res->start, resource_size(res));
1555        if (reg == NULL) {
1556                ret = -ENOMEM;
1557                printk(KERN_ERR "ioremap error.\n");
1558                goto clean_up;
1559        }
1560
1561        /* initialize ucd */
1562        r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL);
1563        if (r8a66597 == NULL) {
1564                ret = -ENOMEM;
1565                printk(KERN_ERR "kzalloc error\n");
1566                goto clean_up;
1567        }
1568
1569        spin_lock_init(&r8a66597->lock);
1570        dev_set_drvdata(&pdev->dev, r8a66597);
1571        r8a66597->pdata = pdev->dev.platform_data;
1572        r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
1573
1574        r8a66597->gadget.ops = &r8a66597_gadget_ops;
1575        device_initialize(&r8a66597->gadget.dev);
1576        dev_set_name(&r8a66597->gadget.dev, "gadget");
1577        r8a66597->gadget.is_dualspeed = 1;
1578        r8a66597->gadget.dev.parent = &pdev->dev;
1579        r8a66597->gadget.dev.dma_mask = pdev->dev.dma_mask;
1580        r8a66597->gadget.dev.release = pdev->dev.release;
1581        r8a66597->gadget.name = udc_name;
1582
1583        init_timer(&r8a66597->timer);
1584        r8a66597->timer.function = r8a66597_timer;
1585        r8a66597->timer.data = (unsigned long)r8a66597;
1586        r8a66597->reg = reg;
1587
1588#ifdef CONFIG_HAVE_CLK
1589        if (r8a66597->pdata->on_chip) {
1590                snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
1591                r8a66597->clk = clk_get(&pdev->dev, clk_name);
1592                if (IS_ERR(r8a66597->clk)) {
1593                        dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
1594                                clk_name);
1595                        ret = PTR_ERR(r8a66597->clk);
1596                        goto clean_up;
1597                }
1598                clk_enable(r8a66597->clk);
1599        }
1600#endif
1601
1602        disable_controller(r8a66597); /* make sure controller is disabled */
1603
1604        ret = request_irq(irq, r8a66597_irq, IRQF_DISABLED | IRQF_SHARED,
1605                        udc_name, r8a66597);
1606        if (ret < 0) {
1607                printk(KERN_ERR "request_irq error (%d)\n", ret);
1608                goto clean_up2;
1609        }
1610
1611        INIT_LIST_HEAD(&r8a66597->gadget.ep_list);
1612        r8a66597->gadget.ep0 = &r8a66597->ep[0].ep;
1613        INIT_LIST_HEAD(&r8a66597->gadget.ep0->ep_list);
1614        for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
1615                struct r8a66597_ep *ep = &r8a66597->ep[i];
1616
1617                if (i != 0) {
1618                        INIT_LIST_HEAD(&r8a66597->ep[i].ep.ep_list);
1619                        list_add_tail(&r8a66597->ep[i].ep.ep_list,
1620                                        &r8a66597->gadget.ep_list);
1621                }
1622                ep->r8a66597 = r8a66597;
1623                INIT_LIST_HEAD(&ep->queue);
1624                ep->ep.name = r8a66597_ep_name[i];
1625                ep->ep.ops = &r8a66597_ep_ops;
1626                ep->ep.maxpacket = 512;
1627        }
1628        r8a66597->ep[0].ep.maxpacket = 64;
1629        r8a66597->ep[0].pipenum = 0;
1630        r8a66597->ep[0].fifoaddr = CFIFO;
1631        r8a66597->ep[0].fifosel = CFIFOSEL;
1632        r8a66597->ep[0].fifoctr = CFIFOCTR;
1633        r8a66597->ep[0].fifotrn = 0;
1634        r8a66597->ep[0].pipectr = get_pipectr_addr(0);
1635        r8a66597->pipenum2ep[0] = &r8a66597->ep[0];
1636        r8a66597->epaddr2ep[0] = &r8a66597->ep[0];
1637
1638        the_controller = r8a66597;
1639
1640        r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep,
1641                                                        GFP_KERNEL);
1642        if (r8a66597->ep0_req == NULL)
1643                goto clean_up3;
1644        r8a66597->ep0_req->complete = nop_completion;
1645
1646        init_controller(r8a66597);
1647
1648        dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1649        return 0;
1650
1651clean_up3:
1652        free_irq(irq, r8a66597);
1653clean_up2:
1654#ifdef CONFIG_HAVE_CLK
1655        if (r8a66597->pdata->on_chip) {
1656                clk_disable(r8a66597->clk);
1657                clk_put(r8a66597->clk);
1658        }
1659#endif
1660clean_up:
1661        if (r8a66597) {
1662                if (r8a66597->ep0_req)
1663                        r8a66597_free_request(&r8a66597->ep[0].ep,
1664                                                r8a66597->ep0_req);
1665                kfree(r8a66597);
1666        }
1667        if (reg)
1668                iounmap(reg);
1669
1670        return ret;
1671}
1672
1673/*-------------------------------------------------------------------------*/
1674static struct platform_driver r8a66597_driver = {
1675        .remove =       __exit_p(r8a66597_remove),
1676        .driver         = {
1677                .name = (char *) udc_name,
1678        },
1679};
1680
1681static int __init r8a66597_udc_init(void)
1682{
1683        return platform_driver_probe(&r8a66597_driver, r8a66597_probe);
1684}
1685module_init(r8a66597_udc_init);
1686
1687static void __exit r8a66597_udc_cleanup(void)
1688{
1689        platform_driver_unregister(&r8a66597_driver);
1690}
1691module_exit(r8a66597_udc_cleanup);
1692
1693MODULE_DESCRIPTION("R8A66597 USB gadget driver");
1694MODULE_LICENSE("GPL");
1695MODULE_AUTHOR("Yoshihiro Shimoda");
1696
1697