From owner-p4-projects@FreeBSD.ORG Sun Dec 10 04:41:37 2006 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 9E46B16A412; Sun, 10 Dec 2006 04:41:37 +0000 (UTC) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 7909A16A40F for ; Sun, 10 Dec 2006 04:41:37 +0000 (UTC) (envelope-from imp@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [69.147.83.41]) by mx1.FreeBSD.org (Postfix) with ESMTP id 36D8B43C9D for ; Sun, 10 Dec 2006 04:40:29 +0000 (GMT) (envelope-from imp@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.6/8.13.6) with ESMTP id kBA4fbA9069142 for ; Sun, 10 Dec 2006 04:41:37 GMT (envelope-from imp@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.6/8.13.4/Submit) id kBA4faGE069138 for perforce@freebsd.org; Sun, 10 Dec 2006 04:41:36 GMT (envelope-from imp@freebsd.org) Date: Sun, 10 Dec 2006 04:41:36 GMT Message-Id: <200612100441.kBA4faGE069138@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to imp@freebsd.org using -f From: Warner Losh To: Perforce Change Reviews Cc: Subject: PERFORCE change 111366 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 10 Dec 2006 04:41:37 -0000 http://perforce.freebsd.org/chv.cgi?CH=111366 Change 111366 by imp@imp_lighthouse on 2006/12/10 04:41:02 Fixes for writing a single byte to IIC as well as robust detection of NACK bit in SR indicating no ACK from the transfer. This is useful when probing the bus. Many of these changes were from tisco based on his experience with the Atmel sample code and the AT91SAM7 processors he's used in the past. These tweaks are somewhat underdocumented in the AT91RM9200 processor manual, but also appear to be hinted at in the errata. As far as I can tell, nothing breaks when we do this, but I've only tested on my KB9202 board. Submitted by: tisco Affected files ... .. //depot/projects/arm/src/sys/arm/at91/at91_twi.c#38 edit Differences ... ==== //depot/projects/arm/src/sys/arm/at91/at91_twi.c#38 (text+ko) ==== @@ -226,7 +226,8 @@ int counter = 100000; uint32_t sr; - while (!((sr = RD4(sc, TWI_SR)) & bit) && counter-- > 0) + while (!((sr = RD4(sc, TWI_SR)) & bit) && counter-- > 0 && + !(sr & TWI_SR_NACK)) continue; if (counter <= 0) err = EBUSY; @@ -300,6 +301,7 @@ int i, len, err; uint32_t rdwr; uint8_t *buf; + uint32_t sr; sc = device_get_softc(dev); err = 0; @@ -318,24 +320,35 @@ WR4(sc, TWI_MMR, TWI_MMR_DADR(msgs[i].slave) | rdwr); len = msgs[i].len; buf = msgs[i].buf; - if (len != 0 && buf == NULL) + /* zero byte transfers aren't allowed */ + if (len == 0 || buf == NULL) return (EINVAL); - WR4(sc, TWI_CR, TWI_CR_START); + if (len == 1) + WR4(sc, TWI_CR, TWI_CR_START | TWI_CR_STOP); + else + WR4(sc, TWI_CR, TWI_CR_START); if (msgs[i].flags & IIC_M_RD) { - while (len--) { - if (len == 0) - WR4(sc, TWI_CR, TWI_CR_STOP); - if ((err = at91_twi_wait(sc, TWI_SR_RXRDY))) - goto out; - *buf++ = RD4(sc, TWI_RHR) & 0xff; + sr = RD4(sc, TWI_SR); + while (!(sr & TWI_SR_TXCOMP)) { + sr = RD4(sc, TWI_SR); + if ((sr = RD4(sc, TWI_SR)) & TWI_SR_RXRDY) { + len--; + *buf++ = RD4(sc, TWI_RHR) & 0xff; + if (len == 0 && msgs[i].len != 1) + WR4(sc, TWI_CR, TWI_CR_STOP); + } + } + if (sr & TWI_SR_NACK) { + err = EADDRNOTAVAIL; + goto out; } } else { while (len--) { - WR4(sc, TWI_THR, *buf++); - if (len == 0) + if (len == 0 && msgs[i].len != 1) WR4(sc, TWI_CR, TWI_CR_STOP); if ((err = at91_twi_wait(sc, TWI_SR_TXRDY))) goto out; + WR4(sc, TWI_THR, *buf++); } } if ((err = at91_twi_wait(sc, TWI_SR_TXCOMP)))