Path: blob/master/arch/powerpc/platforms/powermac/low_i2c.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* arch/powerpc/platforms/powermac/low_i2c.c3*4* Copyright (C) 2003-2005 Ben. Herrenschmidt ([email protected])5*6* The linux i2c layer isn't completely suitable for our needs for various7* reasons ranging from too late initialisation to semantics not perfectly8* matching some requirements of the apple platform functions etc...9*10* This file thus provides a simple low level unified i2c interface for11* powermac that covers the various types of i2c busses used in Apple machines.12* For now, keywest, PMU and SMU, though we could add Cuda, or other bit13* banging busses found on older chipsets in earlier machines if we ever need14* one of them.15*16* The drivers in this file are synchronous/blocking. In addition, the17* keywest one is fairly slow due to the use of msleep instead of interrupts18* as the interrupt is currently used by i2c-keywest. In the long run, we19* might want to get rid of those high-level interfaces to linux i2c layer20* either completely (converting all drivers) or replacing them all with a21* single stub driver on top of this one. Once done, the interrupt will be22* available for our use.23*/2425#undef DEBUG26#undef DEBUG_LOW2728#include <linux/types.h>29#include <linux/sched.h>30#include <linux/init.h>31#include <linux/export.h>32#include <linux/adb.h>33#include <linux/pmu.h>34#include <linux/delay.h>35#include <linux/completion.h>36#include <linux/platform_device.h>37#include <linux/interrupt.h>38#include <linux/timer.h>39#include <linux/mutex.h>40#include <linux/i2c.h>41#include <linux/slab.h>42#include <linux/of_irq.h>43#include <asm/keylargo.h>44#include <asm/uninorth.h>45#include <asm/io.h>46#include <asm/machdep.h>47#include <asm/smu.h>48#include <asm/pmac_pfunc.h>49#include <asm/pmac_low_i2c.h>5051#ifdef DEBUG52#define DBG(x...) do {\53printk(KERN_DEBUG "low_i2c:" x); \54} while(0)55#else56#define DBG(x...)57#endif5859#ifdef DEBUG_LOW60#define DBG_LOW(x...) do {\61printk(KERN_DEBUG "low_i2c:" x); \62} while(0)63#else64#define DBG_LOW(x...)65#endif666768static int pmac_i2c_force_poll = 1;6970/*71* A bus structure. Each bus in the system has such a structure associated.72*/73struct pmac_i2c_bus74{75struct list_head link;76struct device_node *controller;77struct device_node *busnode;78int type;79int flags;80struct i2c_adapter adapter;81void *hostdata;82int channel; /* some hosts have multiple */83int mode; /* current mode */84struct mutex mutex;85int opened;86int polled; /* open mode */87struct platform_device *platform_dev;88struct lock_class_key lock_key;8990/* ops */91int (*open)(struct pmac_i2c_bus *bus);92void (*close)(struct pmac_i2c_bus *bus);93int (*xfer)(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,94u32 subaddr, u8 *data, int len);95};9697static LIST_HEAD(pmac_i2c_busses);9899/*100* Keywest implementation101*/102103struct pmac_i2c_host_kw104{105struct mutex mutex; /* Access mutex for use by106* i2c-keywest */107void __iomem *base; /* register base address */108int bsteps; /* register stepping */109int speed; /* speed */110int irq;111u8 *data;112unsigned len;113int state;114int rw;115int polled;116int result;117struct completion complete;118spinlock_t lock;119struct timer_list timeout_timer;120};121122/* Register indices */123typedef enum {124reg_mode = 0,125reg_control,126reg_status,127reg_isr,128reg_ier,129reg_addr,130reg_subaddr,131reg_data132} reg_t;133134/* The Tumbler audio equalizer can be really slow sometimes */135#define KW_POLL_TIMEOUT (2*HZ)136137/* Mode register */138#define KW_I2C_MODE_100KHZ 0x00139#define KW_I2C_MODE_50KHZ 0x01140#define KW_I2C_MODE_25KHZ 0x02141#define KW_I2C_MODE_DUMB 0x00142#define KW_I2C_MODE_STANDARD 0x04143#define KW_I2C_MODE_STANDARDSUB 0x08144#define KW_I2C_MODE_COMBINED 0x0C145#define KW_I2C_MODE_MODE_MASK 0x0C146#define KW_I2C_MODE_CHAN_MASK 0xF0147148/* Control register */149#define KW_I2C_CTL_AAK 0x01150#define KW_I2C_CTL_XADDR 0x02151#define KW_I2C_CTL_STOP 0x04152#define KW_I2C_CTL_START 0x08153154/* Status register */155#define KW_I2C_STAT_BUSY 0x01156#define KW_I2C_STAT_LAST_AAK 0x02157#define KW_I2C_STAT_LAST_RW 0x04158#define KW_I2C_STAT_SDA 0x08159#define KW_I2C_STAT_SCL 0x10160161/* IER & ISR registers */162#define KW_I2C_IRQ_DATA 0x01163#define KW_I2C_IRQ_ADDR 0x02164#define KW_I2C_IRQ_STOP 0x04165#define KW_I2C_IRQ_START 0x08166#define KW_I2C_IRQ_MASK 0x0F167168/* State machine states */169enum {170state_idle,171state_addr,172state_read,173state_write,174state_stop,175state_dead176};177178#define WRONG_STATE(name) do {\179printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s " \180"(isr: %02x)\n", \181name, __kw_state_names[host->state], isr); \182} while(0)183184static const char *__kw_state_names[] = {185"state_idle",186"state_addr",187"state_read",188"state_write",189"state_stop",190"state_dead"191};192193static inline u8 __kw_read_reg(struct pmac_i2c_host_kw *host, reg_t reg)194{195return readb(host->base + (((unsigned int)reg) << host->bsteps));196}197198static inline void __kw_write_reg(struct pmac_i2c_host_kw *host,199reg_t reg, u8 val)200{201writeb(val, host->base + (((unsigned)reg) << host->bsteps));202(void)__kw_read_reg(host, reg_subaddr);203}204205#define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)206#define kw_read_reg(reg) __kw_read_reg(host, reg)207208static u8 kw_i2c_wait_interrupt(struct pmac_i2c_host_kw *host)209{210int i, j;211u8 isr;212213for (i = 0; i < 1000; i++) {214isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;215if (isr != 0)216return isr;217218/* This code is used with the timebase frozen, we cannot rely219* on udelay nor schedule when in polled mode !220* For now, just use a bogus loop....221*/222if (host->polled) {223for (j = 1; j < 100000; j++)224mb();225} else226msleep(1);227}228return isr;229}230231static void kw_i2c_do_stop(struct pmac_i2c_host_kw *host, int result)232{233kw_write_reg(reg_control, KW_I2C_CTL_STOP);234host->state = state_stop;235host->result = result;236}237238239static void kw_i2c_handle_interrupt(struct pmac_i2c_host_kw *host, u8 isr)240{241u8 ack;242243DBG_LOW("kw_handle_interrupt(%s, isr: %x)\n",244__kw_state_names[host->state], isr);245246if (host->state == state_idle) {247printk(KERN_WARNING "low_i2c: Keywest got an out of state"248" interrupt, ignoring\n");249kw_write_reg(reg_isr, isr);250return;251}252253if (isr == 0) {254printk(KERN_WARNING "low_i2c: Timeout in i2c transfer"255" on keywest !\n");256if (host->state != state_stop) {257kw_i2c_do_stop(host, -EIO);258return;259}260ack = kw_read_reg(reg_status);261if (ack & KW_I2C_STAT_BUSY)262kw_write_reg(reg_status, 0);263host->state = state_idle;264kw_write_reg(reg_ier, 0x00);265if (!host->polled)266complete(&host->complete);267return;268}269270if (isr & KW_I2C_IRQ_ADDR) {271ack = kw_read_reg(reg_status);272if (host->state != state_addr) {273WRONG_STATE("KW_I2C_IRQ_ADDR");274kw_i2c_do_stop(host, -EIO);275}276if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {277host->result = -ENXIO;278host->state = state_stop;279DBG_LOW("KW: NAK on address\n");280} else {281if (host->len == 0)282kw_i2c_do_stop(host, 0);283else if (host->rw) {284host->state = state_read;285if (host->len > 1)286kw_write_reg(reg_control,287KW_I2C_CTL_AAK);288} else {289host->state = state_write;290kw_write_reg(reg_data, *(host->data++));291host->len--;292}293}294kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);295}296297if (isr & KW_I2C_IRQ_DATA) {298if (host->state == state_read) {299*(host->data++) = kw_read_reg(reg_data);300host->len--;301kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);302if (host->len == 0)303host->state = state_stop;304else if (host->len == 1)305kw_write_reg(reg_control, 0);306} else if (host->state == state_write) {307ack = kw_read_reg(reg_status);308if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {309DBG_LOW("KW: nack on data write\n");310host->result = -EFBIG;311host->state = state_stop;312} else if (host->len) {313kw_write_reg(reg_data, *(host->data++));314host->len--;315} else316kw_i2c_do_stop(host, 0);317} else {318WRONG_STATE("KW_I2C_IRQ_DATA");319if (host->state != state_stop)320kw_i2c_do_stop(host, -EIO);321}322kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);323}324325if (isr & KW_I2C_IRQ_STOP) {326kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);327if (host->state != state_stop) {328WRONG_STATE("KW_I2C_IRQ_STOP");329host->result = -EIO;330}331host->state = state_idle;332if (!host->polled)333complete(&host->complete);334}335336/* Below should only happen in manual mode which we don't use ... */337if (isr & KW_I2C_IRQ_START)338kw_write_reg(reg_isr, KW_I2C_IRQ_START);339340}341342/* Interrupt handler */343static irqreturn_t kw_i2c_irq(int irq, void *dev_id)344{345struct pmac_i2c_host_kw *host = dev_id;346unsigned long flags;347348spin_lock_irqsave(&host->lock, flags);349timer_delete(&host->timeout_timer);350kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));351if (host->state != state_idle) {352host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;353add_timer(&host->timeout_timer);354}355spin_unlock_irqrestore(&host->lock, flags);356return IRQ_HANDLED;357}358359static void kw_i2c_timeout(struct timer_list *t)360{361struct pmac_i2c_host_kw *host = timer_container_of(host, t,362timeout_timer);363unsigned long flags;364365spin_lock_irqsave(&host->lock, flags);366367/*368* If the timer is pending, that means we raced with the369* irq, in which case we just return370*/371if (timer_pending(&host->timeout_timer))372goto skip;373374kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));375if (host->state != state_idle) {376host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;377add_timer(&host->timeout_timer);378}379skip:380spin_unlock_irqrestore(&host->lock, flags);381}382383static int kw_i2c_open(struct pmac_i2c_bus *bus)384{385struct pmac_i2c_host_kw *host = bus->hostdata;386mutex_lock(&host->mutex);387return 0;388}389390static void kw_i2c_close(struct pmac_i2c_bus *bus)391{392struct pmac_i2c_host_kw *host = bus->hostdata;393mutex_unlock(&host->mutex);394}395396static int kw_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,397u32 subaddr, u8 *data, int len)398{399struct pmac_i2c_host_kw *host = bus->hostdata;400u8 mode_reg = host->speed;401int use_irq = host->irq && !bus->polled;402403/* Setup mode & subaddress if any */404switch(bus->mode) {405case pmac_i2c_mode_dumb:406return -EINVAL;407case pmac_i2c_mode_std:408mode_reg |= KW_I2C_MODE_STANDARD;409if (subsize != 0)410return -EINVAL;411break;412case pmac_i2c_mode_stdsub:413mode_reg |= KW_I2C_MODE_STANDARDSUB;414if (subsize != 1)415return -EINVAL;416break;417case pmac_i2c_mode_combined:418mode_reg |= KW_I2C_MODE_COMBINED;419if (subsize != 1)420return -EINVAL;421break;422}423424/* Setup channel & clear pending irqs */425kw_write_reg(reg_isr, kw_read_reg(reg_isr));426kw_write_reg(reg_mode, mode_reg | (bus->channel << 4));427kw_write_reg(reg_status, 0);428429/* Set up address and r/w bit, strip possible stale bus number from430* address top bits431*/432kw_write_reg(reg_addr, addrdir & 0xff);433434/* Set up the sub address */435if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB436|| (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)437kw_write_reg(reg_subaddr, subaddr);438439/* Prepare for async operations */440host->data = data;441host->len = len;442host->state = state_addr;443host->result = 0;444host->rw = (addrdir & 1);445host->polled = bus->polled;446447/* Enable interrupt if not using polled mode and interrupt is448* available449*/450if (use_irq) {451/* Clear completion */452reinit_completion(&host->complete);453/* Ack stale interrupts */454kw_write_reg(reg_isr, kw_read_reg(reg_isr));455/* Arm timeout */456host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;457add_timer(&host->timeout_timer);458/* Enable emission */459kw_write_reg(reg_ier, KW_I2C_IRQ_MASK);460}461462/* Start sending address */463kw_write_reg(reg_control, KW_I2C_CTL_XADDR);464465/* Wait for completion */466if (use_irq)467wait_for_completion(&host->complete);468else {469while(host->state != state_idle) {470unsigned long flags;471472u8 isr = kw_i2c_wait_interrupt(host);473spin_lock_irqsave(&host->lock, flags);474kw_i2c_handle_interrupt(host, isr);475spin_unlock_irqrestore(&host->lock, flags);476}477}478479/* Disable emission */480kw_write_reg(reg_ier, 0);481482return host->result;483}484485static struct pmac_i2c_host_kw *__init kw_i2c_host_init(struct device_node *np)486{487struct pmac_i2c_host_kw *host;488const u32 *psteps, *prate, *addrp;489u32 steps;490491host = kzalloc(sizeof(*host), GFP_KERNEL);492if (host == NULL) {493printk(KERN_ERR "low_i2c: Can't allocate host for %pOF\n",494np);495return NULL;496}497498/* Apple is kind enough to provide a valid AAPL,address property499* on all i2c keywest nodes so far ... we would have to fallback500* to macio parsing if that wasn't the case501*/502addrp = of_get_property(np, "AAPL,address", NULL);503if (addrp == NULL) {504printk(KERN_ERR "low_i2c: Can't find address for %pOF\n",505np);506kfree(host);507return NULL;508}509mutex_init(&host->mutex);510init_completion(&host->complete);511spin_lock_init(&host->lock);512timer_setup(&host->timeout_timer, kw_i2c_timeout, 0);513514psteps = of_get_property(np, "AAPL,address-step", NULL);515steps = psteps ? (*psteps) : 0x10;516for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)517steps >>= 1;518/* Select interface rate */519host->speed = KW_I2C_MODE_25KHZ;520prate = of_get_property(np, "AAPL,i2c-rate", NULL);521if (prate) switch(*prate) {522case 100:523host->speed = KW_I2C_MODE_100KHZ;524break;525case 50:526host->speed = KW_I2C_MODE_50KHZ;527break;528case 25:529host->speed = KW_I2C_MODE_25KHZ;530break;531}532host->irq = irq_of_parse_and_map(np, 0);533if (!host->irq)534printk(KERN_WARNING535"low_i2c: Failed to map interrupt for %pOF\n",536np);537538host->base = ioremap((*addrp), 0x1000);539if (host->base == NULL) {540printk(KERN_ERR "low_i2c: Can't map registers for %pOF\n",541np);542kfree(host);543return NULL;544}545546/* Make sure IRQ is disabled */547kw_write_reg(reg_ier, 0);548549/* Request chip interrupt. We set IRQF_NO_SUSPEND because we don't550* want that interrupt disabled between the 2 passes of driver551* suspend or we'll have issues running the pfuncs552*/553if (request_irq(host->irq, kw_i2c_irq, IRQF_NO_SUSPEND,554"keywest i2c", host))555host->irq = 0;556557printk(KERN_INFO "KeyWest i2c @0x%08x irq %d %pOF\n",558*addrp, host->irq, np);559560return host;561}562563564static void __init kw_i2c_add(struct pmac_i2c_host_kw *host,565struct device_node *controller,566struct device_node *busnode,567int channel)568{569struct pmac_i2c_bus *bus;570571bus = kzalloc(sizeof(struct pmac_i2c_bus), GFP_KERNEL);572if (bus == NULL)573return;574575bus->controller = of_node_get(controller);576bus->busnode = of_node_get(busnode);577bus->type = pmac_i2c_bus_keywest;578bus->hostdata = host;579bus->channel = channel;580bus->mode = pmac_i2c_mode_std;581bus->open = kw_i2c_open;582bus->close = kw_i2c_close;583bus->xfer = kw_i2c_xfer;584mutex_init(&bus->mutex);585lockdep_register_key(&bus->lock_key);586lockdep_set_class(&bus->mutex, &bus->lock_key);587if (controller == busnode)588bus->flags = pmac_i2c_multibus;589list_add(&bus->link, &pmac_i2c_busses);590591printk(KERN_INFO " channel %d bus %s\n", channel,592(controller == busnode) ? "<multibus>" : busnode->full_name);593}594595static void __init kw_i2c_probe(void)596{597struct device_node *np, *child, *parent;598599/* Probe keywest-i2c busses */600for_each_compatible_node(np, "i2c","keywest-i2c") {601struct pmac_i2c_host_kw *host;602int multibus;603604/* Found one, init a host structure */605host = kw_i2c_host_init(np);606if (host == NULL)607continue;608609/* Now check if we have a multibus setup (old style) or if we610* have proper bus nodes. Note that the "new" way (proper bus611* nodes) might cause us to not create some busses that are612* kept hidden in the device-tree. In the future, we might613* want to work around that by creating busses without a node614* but not for now615*/616child = of_get_next_child(np, NULL);617multibus = !of_node_name_eq(child, "i2c-bus");618of_node_put(child);619620/* For a multibus setup, we get the bus count based on the621* parent type622*/623if (multibus) {624int chans, i;625626parent = of_get_parent(np);627if (parent == NULL)628continue;629chans = parent->name[0] == 'u' ? 2 : 1;630of_node_put(parent);631for (i = 0; i < chans; i++)632kw_i2c_add(host, np, np, i);633} else {634for_each_child_of_node(np, child) {635const u32 *reg = of_get_property(child,636"reg", NULL);637if (reg == NULL)638continue;639kw_i2c_add(host, np, child, *reg);640}641}642}643}644645646/*647*648* PMU implementation649*650*/651652#ifdef CONFIG_ADB_PMU653654/*655* i2c command block to the PMU656*/657struct pmu_i2c_hdr {658u8 bus;659u8 mode;660u8 bus2;661u8 address;662u8 sub_addr;663u8 comb_addr;664u8 count;665u8 data[];666};667668static void pmu_i2c_complete(struct adb_request *req)669{670complete(req->arg);671}672673static int pmu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,674u32 subaddr, u8 *data, int len)675{676struct adb_request *req = bus->hostdata;677struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req->data[1];678struct completion comp;679int read = addrdir & 1;680int retry;681int rc = 0;682683/* For now, limit ourselves to 16 bytes transfers */684if (len > 16)685return -EINVAL;686687init_completion(&comp);688689for (retry = 0; retry < 16; retry++) {690memset(req, 0, sizeof(struct adb_request));691hdr->bus = bus->channel;692hdr->count = len;693694switch(bus->mode) {695case pmac_i2c_mode_std:696if (subsize != 0)697return -EINVAL;698hdr->address = addrdir;699hdr->mode = PMU_I2C_MODE_SIMPLE;700break;701case pmac_i2c_mode_stdsub:702case pmac_i2c_mode_combined:703if (subsize != 1)704return -EINVAL;705hdr->address = addrdir & 0xfe;706hdr->comb_addr = addrdir;707hdr->sub_addr = subaddr;708if (bus->mode == pmac_i2c_mode_stdsub)709hdr->mode = PMU_I2C_MODE_STDSUB;710else711hdr->mode = PMU_I2C_MODE_COMBINED;712break;713default:714return -EINVAL;715}716717reinit_completion(&comp);718req->data[0] = PMU_I2C_CMD;719req->reply[0] = 0xff;720req->nbytes = sizeof(struct pmu_i2c_hdr) + 1;721req->done = pmu_i2c_complete;722req->arg = ∁723if (!read && len) {724memcpy(hdr->data, data, len);725req->nbytes += len;726}727rc = pmu_queue_request(req);728if (rc)729return rc;730wait_for_completion(&comp);731if (req->reply[0] == PMU_I2C_STATUS_OK)732break;733msleep(15);734}735if (req->reply[0] != PMU_I2C_STATUS_OK)736return -EIO;737738for (retry = 0; retry < 16; retry++) {739memset(req, 0, sizeof(struct adb_request));740741/* I know that looks like a lot, slow as hell, but darwin742* does it so let's be on the safe side for now743*/744msleep(15);745746hdr->bus = PMU_I2C_BUS_STATUS;747748reinit_completion(&comp);749req->data[0] = PMU_I2C_CMD;750req->reply[0] = 0xff;751req->nbytes = 2;752req->done = pmu_i2c_complete;753req->arg = ∁754rc = pmu_queue_request(req);755if (rc)756return rc;757wait_for_completion(&comp);758759if (req->reply[0] == PMU_I2C_STATUS_OK && !read)760return 0;761if (req->reply[0] == PMU_I2C_STATUS_DATAREAD && read) {762int rlen = req->reply_len - 1;763764if (rlen != len) {765printk(KERN_WARNING "low_i2c: PMU returned %d"766" bytes, expected %d !\n", rlen, len);767return -EIO;768}769if (len)770memcpy(data, &req->reply[1], len);771return 0;772}773}774return -EIO;775}776777static void __init pmu_i2c_probe(void)778{779struct pmac_i2c_bus *bus;780struct device_node *busnode;781int channel, sz;782783if (!pmu_present())784return;785786/* There might or might not be a "pmu-i2c" node, we use that787* or via-pmu itself, whatever we find. I haven't seen a machine788* with separate bus nodes, so we assume a multibus setup789*/790busnode = of_find_node_by_name(NULL, "pmu-i2c");791if (busnode == NULL)792busnode = of_find_node_by_name(NULL, "via-pmu");793if (busnode == NULL)794return;795796printk(KERN_INFO "PMU i2c %pOF\n", busnode);797798/*799* We add bus 1 and 2 only for now, bus 0 is "special"800*/801for (channel = 1; channel <= 2; channel++) {802sz = sizeof(struct pmac_i2c_bus) + sizeof(struct adb_request);803bus = kzalloc(sz, GFP_KERNEL);804if (bus == NULL)805return;806807bus->controller = busnode;808bus->busnode = busnode;809bus->type = pmac_i2c_bus_pmu;810bus->channel = channel;811bus->mode = pmac_i2c_mode_std;812bus->hostdata = bus + 1;813bus->xfer = pmu_i2c_xfer;814mutex_init(&bus->mutex);815lockdep_register_key(&bus->lock_key);816lockdep_set_class(&bus->mutex, &bus->lock_key);817bus->flags = pmac_i2c_multibus;818list_add(&bus->link, &pmac_i2c_busses);819820printk(KERN_INFO " channel %d bus <multibus>\n", channel);821}822}823824#endif /* CONFIG_ADB_PMU */825826827/*828*829* SMU implementation830*831*/832833#ifdef CONFIG_PMAC_SMU834835static void smu_i2c_complete(struct smu_i2c_cmd *cmd, void *misc)836{837complete(misc);838}839840static int smu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,841u32 subaddr, u8 *data, int len)842{843struct smu_i2c_cmd *cmd = bus->hostdata;844struct completion comp;845int read = addrdir & 1;846int rc = 0;847848if ((read && len > SMU_I2C_READ_MAX) ||849((!read) && len > SMU_I2C_WRITE_MAX))850return -EINVAL;851852memset(cmd, 0, sizeof(struct smu_i2c_cmd));853cmd->info.bus = bus->channel;854cmd->info.devaddr = addrdir;855cmd->info.datalen = len;856857switch(bus->mode) {858case pmac_i2c_mode_std:859if (subsize != 0)860return -EINVAL;861cmd->info.type = SMU_I2C_TRANSFER_SIMPLE;862break;863case pmac_i2c_mode_stdsub:864case pmac_i2c_mode_combined:865if (subsize > 3 || subsize < 1)866return -EINVAL;867cmd->info.sublen = subsize;868/* that's big-endian only but heh ! */869memcpy(&cmd->info.subaddr, ((char *)&subaddr) + (4 - subsize),870subsize);871if (bus->mode == pmac_i2c_mode_stdsub)872cmd->info.type = SMU_I2C_TRANSFER_STDSUB;873else874cmd->info.type = SMU_I2C_TRANSFER_COMBINED;875break;876default:877return -EINVAL;878}879if (!read && len)880memcpy(cmd->info.data, data, len);881882init_completion(&comp);883cmd->done = smu_i2c_complete;884cmd->misc = ∁885rc = smu_queue_i2c(cmd);886if (rc < 0)887return rc;888wait_for_completion(&comp);889rc = cmd->status;890891if (read && len)892memcpy(data, cmd->info.data, len);893return rc < 0 ? rc : 0;894}895896static void __init smu_i2c_probe(void)897{898struct device_node *controller, *busnode;899struct pmac_i2c_bus *bus;900const u32 *reg;901int sz;902903if (!smu_present())904return;905906controller = of_find_node_by_name(NULL, "smu-i2c-control");907if (controller == NULL)908controller = of_find_node_by_name(NULL, "smu");909if (controller == NULL)910return;911912printk(KERN_INFO "SMU i2c %pOF\n", controller);913914/* Look for childs, note that they might not be of the right915* type as older device trees mix i2c busses and other things916* at the same level917*/918for_each_child_of_node(controller, busnode) {919if (!of_node_is_type(busnode, "i2c") &&920!of_node_is_type(busnode, "i2c-bus"))921continue;922reg = of_get_property(busnode, "reg", NULL);923if (reg == NULL)924continue;925926sz = sizeof(struct pmac_i2c_bus) + sizeof(struct smu_i2c_cmd);927bus = kzalloc(sz, GFP_KERNEL);928if (bus == NULL) {929of_node_put(busnode);930return;931}932933bus->controller = controller;934bus->busnode = of_node_get(busnode);935bus->type = pmac_i2c_bus_smu;936bus->channel = *reg;937bus->mode = pmac_i2c_mode_std;938bus->hostdata = bus + 1;939bus->xfer = smu_i2c_xfer;940mutex_init(&bus->mutex);941lockdep_register_key(&bus->lock_key);942lockdep_set_class(&bus->mutex, &bus->lock_key);943bus->flags = 0;944list_add(&bus->link, &pmac_i2c_busses);945946printk(KERN_INFO " channel %x bus %pOF\n",947bus->channel, busnode);948}949}950951#endif /* CONFIG_PMAC_SMU */952953/*954*955* Core code956*957*/958959960struct pmac_i2c_bus *pmac_i2c_find_bus(struct device_node *node)961{962struct device_node *p = of_node_get(node);963struct device_node *prev = NULL;964struct pmac_i2c_bus *bus;965966while(p) {967list_for_each_entry(bus, &pmac_i2c_busses, link) {968if (p == bus->busnode) {969if (prev && bus->flags & pmac_i2c_multibus) {970const u32 *reg;971reg = of_get_property(prev, "reg",972NULL);973if (!reg)974continue;975if (((*reg) >> 8) != bus->channel)976continue;977}978of_node_put(p);979of_node_put(prev);980return bus;981}982}983of_node_put(prev);984prev = p;985p = of_get_parent(p);986}987return NULL;988}989EXPORT_SYMBOL_GPL(pmac_i2c_find_bus);990991u8 pmac_i2c_get_dev_addr(struct device_node *device)992{993const u32 *reg = of_get_property(device, "reg", NULL);994995if (reg == NULL)996return 0;997998return (*reg) & 0xff;999}1000EXPORT_SYMBOL_GPL(pmac_i2c_get_dev_addr);10011002struct device_node *pmac_i2c_get_controller(struct pmac_i2c_bus *bus)1003{1004return bus->controller;1005}1006EXPORT_SYMBOL_GPL(pmac_i2c_get_controller);10071008struct device_node *pmac_i2c_get_bus_node(struct pmac_i2c_bus *bus)1009{1010return bus->busnode;1011}1012EXPORT_SYMBOL_GPL(pmac_i2c_get_bus_node);10131014int pmac_i2c_get_type(struct pmac_i2c_bus *bus)1015{1016return bus->type;1017}1018EXPORT_SYMBOL_GPL(pmac_i2c_get_type);10191020int pmac_i2c_get_flags(struct pmac_i2c_bus *bus)1021{1022return bus->flags;1023}1024EXPORT_SYMBOL_GPL(pmac_i2c_get_flags);10251026int pmac_i2c_get_channel(struct pmac_i2c_bus *bus)1027{1028return bus->channel;1029}1030EXPORT_SYMBOL_GPL(pmac_i2c_get_channel);103110321033struct i2c_adapter *pmac_i2c_get_adapter(struct pmac_i2c_bus *bus)1034{1035return &bus->adapter;1036}1037EXPORT_SYMBOL_GPL(pmac_i2c_get_adapter);10381039struct pmac_i2c_bus *pmac_i2c_adapter_to_bus(struct i2c_adapter *adapter)1040{1041struct pmac_i2c_bus *bus;10421043list_for_each_entry(bus, &pmac_i2c_busses, link)1044if (&bus->adapter == adapter)1045return bus;1046return NULL;1047}1048EXPORT_SYMBOL_GPL(pmac_i2c_adapter_to_bus);10491050int pmac_i2c_match_adapter(struct device_node *dev, struct i2c_adapter *adapter)1051{1052struct pmac_i2c_bus *bus = pmac_i2c_find_bus(dev);10531054if (bus == NULL)1055return 0;1056return (&bus->adapter == adapter);1057}1058EXPORT_SYMBOL_GPL(pmac_i2c_match_adapter);10591060int pmac_low_i2c_lock(struct device_node *np)1061{1062struct pmac_i2c_bus *bus, *found = NULL;10631064list_for_each_entry(bus, &pmac_i2c_busses, link) {1065if (np == bus->controller) {1066found = bus;1067break;1068}1069}1070if (!found)1071return -ENODEV;1072return pmac_i2c_open(bus, 0);1073}1074EXPORT_SYMBOL_GPL(pmac_low_i2c_lock);10751076int pmac_low_i2c_unlock(struct device_node *np)1077{1078struct pmac_i2c_bus *bus, *found = NULL;10791080list_for_each_entry(bus, &pmac_i2c_busses, link) {1081if (np == bus->controller) {1082found = bus;1083break;1084}1085}1086if (!found)1087return -ENODEV;1088pmac_i2c_close(bus);1089return 0;1090}1091EXPORT_SYMBOL_GPL(pmac_low_i2c_unlock);109210931094int pmac_i2c_open(struct pmac_i2c_bus *bus, int polled)1095{1096int rc;10971098mutex_lock(&bus->mutex);1099bus->polled = polled || pmac_i2c_force_poll;1100bus->opened = 1;1101bus->mode = pmac_i2c_mode_std;1102if (bus->open && (rc = bus->open(bus)) != 0) {1103bus->opened = 0;1104mutex_unlock(&bus->mutex);1105return rc;1106}1107return 0;1108}1109EXPORT_SYMBOL_GPL(pmac_i2c_open);11101111void pmac_i2c_close(struct pmac_i2c_bus *bus)1112{1113WARN_ON(!bus->opened);1114if (bus->close)1115bus->close(bus);1116bus->opened = 0;1117mutex_unlock(&bus->mutex);1118}1119EXPORT_SYMBOL_GPL(pmac_i2c_close);11201121int pmac_i2c_setmode(struct pmac_i2c_bus *bus, int mode)1122{1123WARN_ON(!bus->opened);11241125/* Report me if you see the error below as there might be a new1126* "combined4" mode that I need to implement for the SMU bus1127*/1128if (mode < pmac_i2c_mode_dumb || mode > pmac_i2c_mode_combined) {1129printk(KERN_ERR "low_i2c: Invalid mode %d requested on"1130" bus %pOF !\n", mode, bus->busnode);1131return -EINVAL;1132}1133bus->mode = mode;11341135return 0;1136}1137EXPORT_SYMBOL_GPL(pmac_i2c_setmode);11381139int pmac_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,1140u32 subaddr, u8 *data, int len)1141{1142int rc;11431144WARN_ON(!bus->opened);11451146DBG("xfer() chan=%d, addrdir=0x%x, mode=%d, subsize=%d, subaddr=0x%x,"1147" %d bytes, bus %pOF\n", bus->channel, addrdir, bus->mode, subsize,1148subaddr, len, bus->busnode);11491150rc = bus->xfer(bus, addrdir, subsize, subaddr, data, len);11511152#ifdef DEBUG1153if (rc)1154DBG("xfer error %d\n", rc);1155#endif1156return rc;1157}1158EXPORT_SYMBOL_GPL(pmac_i2c_xfer);11591160/* some quirks for platform function decoding */1161enum {1162pmac_i2c_quirk_invmask = 0x00000001u,1163pmac_i2c_quirk_skip = 0x00000002u,1164};11651166static void pmac_i2c_devscan(void (*callback)(struct device_node *dev,1167int quirks))1168{1169struct pmac_i2c_bus *bus;1170struct device_node *np;1171static struct whitelist_ent {1172char *name;1173char *compatible;1174int quirks;1175} whitelist[] = {1176/* XXX Study device-tree's & apple drivers are get the quirks1177* right !1178*/1179/* Workaround: It seems that running the clockspreading1180* properties on the eMac will cause lockups during boot.1181* The machine seems to work fine without that. So for now,1182* let's make sure i2c-hwclock doesn't match about "imic"1183* clocks and we'll figure out if we really need to do1184* something special about those later.1185*/1186{ "i2c-hwclock", "imic5002", pmac_i2c_quirk_skip },1187{ "i2c-hwclock", "imic5003", pmac_i2c_quirk_skip },1188{ "i2c-hwclock", NULL, pmac_i2c_quirk_invmask },1189{ "i2c-cpu-voltage", NULL, 0},1190{ "temp-monitor", NULL, 0 },1191{ "supply-monitor", NULL, 0 },1192{ NULL, NULL, 0 },1193};11941195/* Only some devices need to have platform functions instantiated1196* here. For now, we have a table. Others, like 9554 i2c GPIOs used1197* on Xserve, if we ever do a driver for them, will use their own1198* platform function instance1199*/1200list_for_each_entry(bus, &pmac_i2c_busses, link) {1201for_each_child_of_node(bus->busnode, np) {1202struct whitelist_ent *p;1203/* If multibus, check if device is on that bus */1204if (bus->flags & pmac_i2c_multibus)1205if (bus != pmac_i2c_find_bus(np))1206continue;1207for (p = whitelist; p->name != NULL; p++) {1208if (!of_node_name_eq(np, p->name))1209continue;1210if (p->compatible &&1211!of_device_is_compatible(np, p->compatible))1212continue;1213if (p->quirks & pmac_i2c_quirk_skip)1214break;1215callback(np, p->quirks);1216break;1217}1218}1219}1220}12211222#define MAX_I2C_DATA 6412231224struct pmac_i2c_pf_inst1225{1226struct pmac_i2c_bus *bus;1227u8 addr;1228u8 buffer[MAX_I2C_DATA];1229u8 scratch[MAX_I2C_DATA];1230int bytes;1231int quirks;1232};12331234static void* pmac_i2c_do_begin(struct pmf_function *func, struct pmf_args *args)1235{1236struct pmac_i2c_pf_inst *inst;1237struct pmac_i2c_bus *bus;12381239bus = pmac_i2c_find_bus(func->node);1240if (bus == NULL) {1241printk(KERN_ERR "low_i2c: Can't find bus for %pOF (pfunc)\n",1242func->node);1243return NULL;1244}1245if (pmac_i2c_open(bus, 0)) {1246printk(KERN_ERR "low_i2c: Can't open i2c bus for %pOF (pfunc)\n",1247func->node);1248return NULL;1249}12501251/* XXX might need GFP_ATOMIC when called during the suspend process,1252* but then, there are already lots of issues with suspending when1253* near OOM that need to be resolved, the allocator itself should1254* probably make GFP_NOIO implicit during suspend1255*/1256inst = kzalloc(sizeof(struct pmac_i2c_pf_inst), GFP_KERNEL);1257if (inst == NULL) {1258pmac_i2c_close(bus);1259return NULL;1260}1261inst->bus = bus;1262inst->addr = pmac_i2c_get_dev_addr(func->node);1263inst->quirks = (int)(long)func->driver_data;1264return inst;1265}12661267static void pmac_i2c_do_end(struct pmf_function *func, void *instdata)1268{1269struct pmac_i2c_pf_inst *inst = instdata;12701271if (inst == NULL)1272return;1273pmac_i2c_close(inst->bus);1274kfree(inst);1275}12761277static int pmac_i2c_do_read(PMF_STD_ARGS, u32 len)1278{1279struct pmac_i2c_pf_inst *inst = instdata;12801281inst->bytes = len;1282return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 0, 0,1283inst->buffer, len);1284}12851286static int pmac_i2c_do_write(PMF_STD_ARGS, u32 len, const u8 *data)1287{1288struct pmac_i2c_pf_inst *inst = instdata;12891290return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,1291(u8 *)data, len);1292}12931294/* This function is used to do the masking & OR'ing for the "rmw" type1295* callbacks. Ze should apply the mask and OR in the values in the1296* buffer before writing back. The problem is that it seems that1297* various darwin drivers implement the mask/or differently, thus1298* we need to check the quirks first1299*/1300static void pmac_i2c_do_apply_rmw(struct pmac_i2c_pf_inst *inst,1301u32 len, const u8 *mask, const u8 *val)1302{1303int i;13041305if (inst->quirks & pmac_i2c_quirk_invmask) {1306for (i = 0; i < len; i ++)1307inst->scratch[i] = (inst->buffer[i] & mask[i]) | val[i];1308} else {1309for (i = 0; i < len; i ++)1310inst->scratch[i] = (inst->buffer[i] & ~mask[i])1311| (val[i] & mask[i]);1312}1313}13141315static int pmac_i2c_do_rmw(PMF_STD_ARGS, u32 masklen, u32 valuelen,1316u32 totallen, const u8 *maskdata,1317const u8 *valuedata)1318{1319struct pmac_i2c_pf_inst *inst = instdata;13201321if (masklen > inst->bytes || valuelen > inst->bytes ||1322totallen > inst->bytes || valuelen > masklen)1323return -EINVAL;13241325pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);13261327return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,1328inst->scratch, totallen);1329}13301331static int pmac_i2c_do_read_sub(PMF_STD_ARGS, u8 subaddr, u32 len)1332{1333struct pmac_i2c_pf_inst *inst = instdata;13341335inst->bytes = len;1336return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 1, subaddr,1337inst->buffer, len);1338}13391340static int pmac_i2c_do_write_sub(PMF_STD_ARGS, u8 subaddr, u32 len,1341const u8 *data)1342{1343struct pmac_i2c_pf_inst *inst = instdata;13441345return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,1346subaddr, (u8 *)data, len);1347}13481349static int pmac_i2c_do_set_mode(PMF_STD_ARGS, int mode)1350{1351struct pmac_i2c_pf_inst *inst = instdata;13521353return pmac_i2c_setmode(inst->bus, mode);1354}13551356static int pmac_i2c_do_rmw_sub(PMF_STD_ARGS, u8 subaddr, u32 masklen,1357u32 valuelen, u32 totallen, const u8 *maskdata,1358const u8 *valuedata)1359{1360struct pmac_i2c_pf_inst *inst = instdata;13611362if (masklen > inst->bytes || valuelen > inst->bytes ||1363totallen > inst->bytes || valuelen > masklen)1364return -EINVAL;13651366pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);13671368return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,1369subaddr, inst->scratch, totallen);1370}13711372static int pmac_i2c_do_mask_and_comp(PMF_STD_ARGS, u32 len,1373const u8 *maskdata,1374const u8 *valuedata)1375{1376struct pmac_i2c_pf_inst *inst = instdata;1377int i, match;13781379/* Get return value pointer, it's assumed to be a u32 */1380if (!args || !args->count || !args->u[0].p)1381return -EINVAL;13821383/* Check buffer */1384if (len > inst->bytes)1385return -EINVAL;13861387for (i = 0, match = 1; match && i < len; i ++)1388if ((inst->buffer[i] & maskdata[i]) != valuedata[i])1389match = 0;1390*args->u[0].p = match;1391return 0;1392}13931394static int pmac_i2c_do_delay(PMF_STD_ARGS, u32 duration)1395{1396msleep((duration + 999) / 1000);1397return 0;1398}139914001401static struct pmf_handlers pmac_i2c_pfunc_handlers = {1402.begin = pmac_i2c_do_begin,1403.end = pmac_i2c_do_end,1404.read_i2c = pmac_i2c_do_read,1405.write_i2c = pmac_i2c_do_write,1406.rmw_i2c = pmac_i2c_do_rmw,1407.read_i2c_sub = pmac_i2c_do_read_sub,1408.write_i2c_sub = pmac_i2c_do_write_sub,1409.rmw_i2c_sub = pmac_i2c_do_rmw_sub,1410.set_i2c_mode = pmac_i2c_do_set_mode,1411.mask_and_compare = pmac_i2c_do_mask_and_comp,1412.delay = pmac_i2c_do_delay,1413};14141415static void __init pmac_i2c_dev_create(struct device_node *np, int quirks)1416{1417DBG("dev_create(%pOF)\n", np);14181419pmf_register_driver(np, &pmac_i2c_pfunc_handlers,1420(void *)(long)quirks);1421}14221423static void __init pmac_i2c_dev_init(struct device_node *np, int quirks)1424{1425DBG("dev_create(%pOF)\n", np);14261427pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_INIT, NULL);1428}14291430static void pmac_i2c_dev_suspend(struct device_node *np, int quirks)1431{1432DBG("dev_suspend(%pOF)\n", np);1433pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_SLEEP, NULL);1434}14351436static void pmac_i2c_dev_resume(struct device_node *np, int quirks)1437{1438DBG("dev_resume(%pOF)\n", np);1439pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_WAKE, NULL);1440}14411442void pmac_pfunc_i2c_suspend(void)1443{1444pmac_i2c_devscan(pmac_i2c_dev_suspend);1445}14461447void pmac_pfunc_i2c_resume(void)1448{1449pmac_i2c_devscan(pmac_i2c_dev_resume);1450}14511452/*1453* Initialize us: probe all i2c busses on the machine, instantiate1454* busses and platform functions as needed.1455*/1456/* This is non-static as it might be called early by smp code */1457int __init pmac_i2c_init(void)1458{1459static int i2c_inited;14601461if (i2c_inited)1462return 0;1463i2c_inited = 1;14641465/* Probe keywest-i2c busses */1466kw_i2c_probe();14671468#ifdef CONFIG_ADB_PMU1469/* Probe PMU i2c busses */1470pmu_i2c_probe();1471#endif14721473#ifdef CONFIG_PMAC_SMU1474/* Probe SMU i2c busses */1475smu_i2c_probe();1476#endif14771478/* Now add platform functions for some known devices */1479pmac_i2c_devscan(pmac_i2c_dev_create);14801481return 0;1482}1483machine_arch_initcall(powermac, pmac_i2c_init);14841485/* Since pmac_i2c_init can be called too early for the platform device1486* registration, we need to do it at a later time. In our case, subsys1487* happens to fit well, though I agree it's a bit of a hack...1488*/1489static int __init pmac_i2c_create_platform_devices(void)1490{1491struct pmac_i2c_bus *bus;1492int i = 0;14931494/* In the case where we are initialized from smp_init(), we must1495* not use the timer (and thus the irq). It's safe from now on1496* though1497*/1498pmac_i2c_force_poll = 0;14991500/* Create platform devices */1501list_for_each_entry(bus, &pmac_i2c_busses, link) {1502bus->platform_dev =1503platform_device_alloc("i2c-powermac", i++);1504if (bus->platform_dev == NULL)1505return -ENOMEM;1506bus->platform_dev->dev.platform_data = bus;1507bus->platform_dev->dev.of_node = bus->busnode;1508platform_device_add(bus->platform_dev);1509}15101511/* Now call platform "init" functions */1512pmac_i2c_devscan(pmac_i2c_dev_init);15131514return 0;1515}1516machine_subsys_initcall(powermac, pmac_i2c_create_platform_devices);151715181519